[Solved] Doctest python

30 $

File Name: Doctest_python.zip
File Size: 131.88 KB

SKU: [Solved] Doctest python Category: Tag:

Or Upload Your Assignment Here:


import doctest

def get_digits(s):”'(str) -> strParameter s is a string of non-negative ints and words separated by spaces.Return a new string containing all the ints in s separated by spaces. Hint: You mayneed to take care of removing repeated spaces in your final string.

>>> get_digits(‘1 word 2 words 3 more’)‘1 2 3′>>> get_digits(’10 characters 20 30 more’)’10 20 30′

”’pass # replace with your code

def max_numbers(s):”'(str) -> intAssume s is a string of non-negative integers seperated by spaces. Return the max ofthe integers in s.

Requirement: Do NOT use lists (or any of your otherfunctions). You must use a for loop. You may assume s is non-empty.

>>> max_numbers(’45’)45>>> max_numbers(’34 3 542 11′)542”’pass # replace with your code

def max_ints_mixed(s):”'(str) -> intAssume s is a string of non-negative integers and words separated by spaces. Returnthe max of the integers in s.

Requirements: You may NOT use lists. Your solutions should be ONE LINE of code. Youshould call some of the functions in this exercise. You may assume there is at leastone int in the string and ints are separated by spaces, for example, ‘a20’ is notconsidered to be an int.

>>> max_ints_mixed(‘3’)3>>> max_ints_mixed(‘my sister is 10 years and 11 months old.’)11”’pass # replace with your code

# Now lets do it again with lists! Lists make it easier!!

def get_digits_list(mixed_list):”'(list of str) -> list of intParameter mixed_list is a list of strs representing words andnon-negative ints. Return a new list of integers containingthe strs representing ints from mixed_list, converted to ints.

>>> get_digits_list([’45’, ‘banana’, ‘3’, ‘2001’, ‘3 apples’])[45, 3, 2001]>>> get_digits_list([‘banana’, ‘apple’])[]>>> get_digits_list([‘3′, ’15’, ’16’])[3, 15, 16]>>> get_digits_list([])[]”’pass # replace with your code

def max_ints_mixed2(s):”'(str) -> intAssume s is a str of words and non-negative ints separated by spaces. Return themax of the ints in s.

Requirement: Your function must be only 1 line long. You should use lists and yourget_digits_list function. You may assume there is at least one int in the string andints are separated by spaces, for example, ‘a20′ does not have any ints in it.

>>> max_ints_mixed2(“45 silly 80”)80>>> max_ints_mixed2(“There are 350 students in csca20 and 900 in csca67″)900”’

pass # replace with your code

if __name__ == ‘__main__’:

doctest.testmod(verbose = True)

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] Doctest python
30 $