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') 1 last2('xaxxaxaxx') 1 last2('axxxaaxx') 2 My solution: def last2 ( str ) : if len ( str ) < 2 : return 0 last2 = str [ len ( str ) - 2 : ] count = 0 for i in range ( len ( str ) - 2 ) : sub = str [ i:i+ 2 ] if sub == last2: count = count + 1 return count 6. array_count9 Given an array of ints, return the number of 9's in the array.

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern