codingbat-python-soru-cevap-2

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

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

View Full Document Right Arrow Icon
Codingbat Python Questions and Answers Section 2 This document is prepared and can be used only for educational purposes. All questions are taken from http:/ /codingbat.com which contains great questions about Python and Java. Please go to original website and solve questions there. Codingbat also presents a report tool which educators can see students’ results. The answers are done by me in my spare times, Codingbat says all answers are true. I used some of questions in internship application reviews. You can use questions , see http://codingbat.com for usage details. You can use answers of me or this document it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Document is prepared with Google Docs and syntax highlighter is GeSHi , which is a great open source tool. If you have any questions, please email me via samet2@gmail.com Have fun with Python! Samet Atdag This document contains 27 questions in these sections: Warmup-2 Logic-2 String-2 List-2 Warmup-2 1. string_times 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'
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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')
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online