搜尋此網誌

2026年6月2日星期二

Part I of Python test

def encodeString(stringVal):

    encodedList = []

    prevChar = stringVal[0]

    count = 0


    for char in stringVal:

        if prevChar != char:

            encodedList.append((prevChar, count))

            count = 0

            prevChar = char

        count = count + 1


    encodedList.append((prevChar, count))

    return encodedList


Step 1: Setup

  • encodedList = [] → empty list to store results.

  • prevChar = stringVal[0] → start with the first character of the string.

  • count = 0 → counter for consecutive occurrences.


Step 2: Loop through each character

for char in stringVal:

Goes through every character in the string one by one.


Step 3: Check if the character changes

if prevChar != char:
    encodedList.append((prevChar, count))
    count = 0
    prevChar = char

If the current character is different from the previous one:

  • Save the previous character and its count into encodedList.

  • Reset count to 0.

  • Update prevChar to the new character.


Step 4: Count occurrences

count = count + 1

Always increment the counter for the current character.


Step 5: Add the last group

encodedList.append((prevChar, count))

After the loop finishes, add the final character group to the list.


Step 6: Return result

return encodedList

The function returns the list of (character, count) tuples.

Microsoft Copilot

Naughty

catnap: a short sleep

pickled: (of food) preserved in vinegar

chile con queso: (in Tex-Mex cooking) a thick sauce of melted cheese seasoned with chilli peppers, typically served warm as a dip for tortilla chips

bequeath: pass (something) on or leave (something) to someone else

Plymouth was a beloved American automobile brand produced by the Chrysler Corporation from 1928 until 2001.

tow: to pull a car, boat, etc. behind another vehicle, using a rope or chain

junkyard: a place where old cars, machines, etc. are collected, so that parts of them, or the metal they are made of, can be sold to be used again

a chunk of: (informal) a fairly large amount of something

fetch: to go to where somebody/something is and bring them/it back

cruising: (of a car, etc. or its driver) to drive along slowly, especially when you are looking at or for something

rear-wheel drive: a system in which power from the engine is sent to the back wheels of a vehicle

fishtail: if a vehicle fishtails, the back end slides from side to side

joyriding: the crime of stealing a car and driving it for pleasure, usually in a fast and dangerous way

barbed wire: strong wire with short sharp points on it, used especially for fences

bail out: to escape from a situation that you no longer want to be involved in

pint: a pint of beer (especially in a pub)

foible: a silly habit or a strange or weak aspect of a person’s character that is not considered serious by other people

frenzy: a state of great activity and strong emotion that is often violent or frightening and not under control

attribute something to something: to say or believe that something is the result of a particular thing

intimidate: to frighten or threaten somebody so that they will do what you want

deferential: showing that you respect somebody/something, especially somebody older or more senior than you

"To get a kick out of" is an idiom that means to really enjoy something, or to get a strong feeling of excitement and pleasure from doing or experiencing it.

bemused: showing that you are confused and unable to think clearly

bide: to stay or live in a place

insubordination: the act of refusing to obey orders or show respect for somebody who has a higher rank

Bill Gates "Source Code"

Online Dictionaries Used:

hk.dictionary.search.yahoo.com

www.oxfordlearnersdictionaries.com

Google AI overview