codingbat-python-soru-cevap-2

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

Info icon This 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 [email protected] 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'
Image of page 1

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

View Full Document Right 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
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') 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.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern