python练习题园地(一)——Warmup篇
最近找到一个网站:https://codingbat.com/python,这里是python和java初学者的园地,在这里你可以充分的发挥你在程序学习上的逻辑推理能力。一关一关的过,做你没做过或是曾做过后练习题,在学习的同时还可以熟悉一下英文,大多数都可以看懂。
Warmup-1:
Warmup-1 > sleep_in:
The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return True if we sleep in.
sleep_in(False, False) → True
sleep_in(True, False) → False
sleep_in(False, True) → True
1 | def sleep_in(weekday, vacation): |
Warmup-1 > monkey_trouble
We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble.
monkey_trouble(True, True) → True
monkey_trouble(False, False) → True
monkey_trouble(True, False) → False
1 | def monkey_trouble(a_smile, b_smile): |
Warmup-1 > sum_double
Given two int values, return their sum. Unless the two values are the same, then return double their sum.
sum_double(1, 2) → 3
sum_double(3, 2) → 5
sum_double(2, 2) → 8
1 | def sum_double(a, b): |
Warmup-1 > diff21
Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.
diff21(19) → 2
diff21(10) → 11
diff21(21) → 0
1 | def diff21(n): |
Warmup-1 > parrot_trouble
We have a loud talking parrot. The “hour” parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble.
parrot_trouble(True, 6) → True
parrot_trouble(True, 7) → False
parrot_trouble(False, 6) → False
1 | def parrot_trouble(talking, hour): |
Warmup-1 > makes10
Given 2 ints, a and b, return True if one if them is 10 or if their sum is 10.
makes10(9, 10) → True
makes10(9, 9) → False
makes10(1, 9) → True
1 | def makes10(a, b): |
简洁版(官方版),仔细思量:
1 | def makes10(a, b): |
Warmup-1 > near_hundred
Given an int n, return True if it is within 10 of 100 or 200. Note: abs(num) computes the absolute value of a number.
near_hundred(93) → True
near_hundred(90) → True
near_hundred(89) → False
1 | def near_hundred(n): |
官方版:
1 | def near_hundred(n): |
Warmup-1 > pos_neg
Given 2 int values, return True if one is negative(负数) and one is positive(正数). Except if the parameter “negative” is True, then return True only if both are negative.
pos_neg(1, -1, False) → True
pos_neg(-1, 1, False) → True
pos_neg(-4, -5, True) → True
1 | def pos_neg(a, b, negative): |
官方版:
1 | def pos_neg(a, b, negative): |
Warmup-1 > not_string
Given a string, return a new string where “not “ has been added to the front. However, if the string already begins with “not”, return the string unchanged. (给定一个字符串,返回一个新字符串,将“not ”添加到字符串开头。但是,如果字符串已经以“not”开头,则不加更改地返回字符串。)
not_string(‘candy’) → ‘not candy’
not_string(‘x’) → ‘not x’
not_string(‘not bad’) → ‘not bad’
1 | def not_string(str): |
官方版:
1 | def not_string(str): |
Warmup-1 > missing_char
Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..len(str)-1 inclusive).
missing_char(‘kitten’, 1) → ‘ktten’
missing_char(‘kitten’, 0) → ‘itten’
missing_char(‘kitten’, 4) → ‘kittn’
1 | def missing_char(str, n): |
官方版:
1 | def missing_char(str, n): |
Warmup-1 > front_back
Given a string, return a new string where the first and last chars have been exchanged. (返回首尾字符交换的字符串)
front_back(‘code’) → ‘eodc’
front_back(‘a’) → ‘a’
front_back(‘ab’) → ‘ba’
以下代码本机测试通过(考虑字符串只有1,或0时):
1 | def front_back(str): |
官方版:
1 | def front_back(str): |
Warmup-1 > front3
Given a string, we’ll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
front3(‘Java’) → ‘JavJavJav’
front3(‘Chocolate’) → ‘ChoChoCho’
front3(‘abc’) → ‘abcabcabc’
1 | def front3(str): |
官方代码:
1 | def front3(str): |
Warmup-2
Warmup-2 > 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’
1 | def string_times(str, n): |
官方代码:
1 | def string_times(str, n): |
Warmup-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;(返回字符串的前3个字符的N个副本,小于3则返回3个)
front_times(‘Chocolate’, 2) → ‘ChoCho’
front_times(‘Chocolate’, 3) → ‘ChoChoCho’
front_times(‘Abc’, 3) → ‘AbcAbcAbc’
1 | def front_times(str, n): |
官方代码:
1 | def front_times(str, n): |
Warmup-2 > 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’
1 | def string_bits(str): |
官方代码:
1 | def string_bits(str): |
Warmup-2 > 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’
1 | def string_splosion(str): |
官方代码:
1 | def string_splosion(str): |
Warmup-2 > 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
1 | def last2(str): |
官方代码:
1 | def last2(str): |
Warmup-2 > array_count9
Given an array of ints, return the number of 9’s in the array.(返回整数数组中9出现的次数)
array_count9([1, 2, 9]) → 1
array_count9([1, 9, 9]) → 2
array_count9([1, 9, 9, 3, 9]) → 3
1 | def array_count9(nums): |
官方代码一致,唯变量不同:
Warmup-2 > array_front9
Given an array of ints, return True if one of the first 4 elements in the array is a 9. The array length may be less than 4. (返回整数数组中前4位数中出现9的布尔值)
array_front9([1, 2, 9, 3, 4]) → True
array_front9([1, 2, 3, 4, 9]) → False
array_front9([1, 2, 3, 4, 5]) → False
1 | def array_front9(nums): |
官方代码:
1 | def array_front9(nums): |
Warmup-2 > array123
Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere.
array123([1, 1, 2, 3, 1]) → True
array123([1, 1, 2, 4, 1]) → False
array123([1, 1, 2, 1, 2, 3]) → True
1 | def array123(nums): |
官方代码:
1 | def array123(nums): |
Warmup-2 > string_match
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
1 | def string_match(a, b): |
官方代码:
1 | def string_match(a, b): |
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com