codingbat-python-soru-cevap-2

# codingbat-python-soru-cevap-2 - Codingbat Python Questions...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
My solution: def string_times ( str , n ) : result = '' i= 0 while i < n: result += str i += 1 return result 2. front_times 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' My solution: def front_times ( str , n ) : if len ( str ) < 3 : front = str else : front = str [ : 3 ] result = '' for i in range ( n ) : result += front return result 3. string_bits 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' My solution: def string_bits ( str ) : result = "" for i in range ( 0 , len ( str )) : if i % 2 == 0 : result = result + str [ i ] return result
4. string_splosion Given a non-empty string like "Code" return a string like "CCoCodCode". string_splosion('Code') 'CCoCodCode' string_splosion('abc') 'aababc' string_splosion('ab') 'aab' My solution: def string_splosion ( str ) : result = '' for i in range ( len ( str ) + 1 ) : result += str [ :i ] return result 5. last2 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')

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/17/2010 for the course CS ANS taught by Professor Brereton during the Spring '10 term at University of Texas.

### Page1 / 13

codingbat-python-soru-cevap-2 - Codingbat Python Questions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online