Homework 6 Coding Bat

Ace your homework & exams now with Quizwiz!

Process Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front; front_times('Chocolate', 2) → 'ChoCho' front_times('Chocolate', 3) → 'ChoChoCho' front_times('Abc', 3) → 'AbcAbcAbc'

- define number in length (3) - define what happens if string is less than 3 - define length of returned name - initialize result - for loop bc return "n" amount of times - return result

Process Given a string and a non-negative int n, return a larger string that is n copies of the original string. string_times('Hi', 2) → 'HiHi' string_times('Hi', 3) → 'HiHiHi' string_times('Hi', 1) → 'Hi'

- initialize result - for loop bc "n" copies - result is result plus another string (loops "n" times) - return result

Process Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo". string_bits('Hello') → 'Hlo' string_bits('Hi') → 'H' string_bits('Heeololeo') → 'Hello'

- initialize result - for loop bc execute for every letter in str - if statement if even number then return result plus str[i] - return result

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front; front_times('Chocolate', 2) → 'ChoCho' front_times('Chocolate', 3) → 'ChoChoCho' front_times('Abc', 3) → 'AbcAbcAbc'

def front_times(str, n): front_len = 3 if front_len > len(str): front_len = len(str) front = str[:front_len] result = "" for i in range(n): result = result + front return result

Given a string, return the count of the number of times that a substring length 2 appears in the string and also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the end substring). last2('hixxhi') → 1 last2('xaxxaxaxx') → 1 last2('axxxaaxx') → 2

def last2(str): if len(str)<2: return 0 last=str[len(str)-2:] count=0 for i in range(len(str)-2): sub=str[i:i+2] if sub==last: count=count+1 return count

Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo". string_bits('Hello') → 'Hlo' string_bits('Hi') → 'H' string_bits('Heeololeo') → 'Hello'

def string_bits(str): result= "" for i in range(len(str)): if i%2==0: result=result+str[i] return result

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. string_match('xxcaazz', 'xxbaaz') → 3 string_match('abc', 'abc') → 2 string_match('abc', 'axc') → 0

def string_match(a, b): shorter= min(len(a), len(b)) count=0 for i in range(shorter-1): a1=a[i:i+2] b1=b[i:i+2] if a1==b1: count=count+1 return count

Given a non-empty string like "Code" return a string like "CCoCodCode". string_splosion('Code') → 'CCoCodCode' string_splosion('abc') → 'aababc' string_splosion('ab') → 'aab'

def string_splosion(str): result="" for i in range(len(str)): result=result+str[:i+1] return result

Given a string and a non-negative int n, return a larger string that is n copies of the original string. string_times('Hi', 2) → 'HiHi' string_times('Hi', 3) → 'HiHiHi' string_times('Hi', 1) → 'Hi'

def string_times(str, n): result= "" for i in range(n): result=result+str return result


Related study sets

Chapter 26: The home inspection report

View Set

Principles of Finance - Last Chapters

View Set

Jeu téléphone - Choisissez la bonne réponse.

View Set

Chapter 1: Atoms and Molecules Quiz

View Set

Real Estate Express Chapter 6 ❤️

View Set

Ch. 10: Visually Identifying the five stages of Mitosis

View Set

Practice Questions Ch 1, 2, 10, 11, 12

View Set

Chapter 38: Agents to Control Blood Glucose Levels

View Set

SPANISH II - buscar= to look for

View Set

Math models and applications pt. 3

View Set