Our function (sum_numbers) will take a string as an argument; for example
10 abc 20 de44 30 55fg 40
Given that input, our function should return 100. That’s because the function will ignore any word that contains nondigits.
For this exercise, write a function (join_numbers) that takes a range of integers. The function should return those numbers as a string, with commas between the numbers. That is, given range(15) as input, the function should return this string:
Given the following dataset, can you see if there’s a significant difference between the conversion rate of users between the test and control group? The relevant columns in the table are conversion and test. The conversion column has values of 0 and 1 which represent if the user converted (1)…
In this exercise, we’re going to create a password-generation function. Actually, we’re going to create a factory for password-generation functions. That is, I might need to generate a large number of passwords, all of which use the same set of characters. (You know how it is. Some applications require a mix of capital letters, lowercase letters, numbers, and symbols; whereas others require that you only use letters; and still others allow both letters and digits.) You’ll thus call the function create_password _generator with a string. That string will return a function, which itself takes an integer argument. Calling this function will return a password of the specified length, using the string from which it was created; for example
alpha_password = create_password_generator('abcdef')
symbol_password = create_password_generator('!@#$%')print(alpha_password(5)) # efeaa
print(alpha_password(10)) # cacdacbadaprint(symbol_password(5)) # %#@%@
print(symbol_password(10)) # @!%%$%$%%#
For this exercise, I want you to write a function (calc) that expects a single argument — a string containing a simple math expression in prefix notation — with an operator and two numbers. Your program will parse the input and produce the appropriate output. For our purposes, it’s enough to handle the six basic arithmetic operations in Python: addition, subtraction, multiplication, division (/), modulus (%), and exponentiation (**). The normal Python math rules should work, such that division always results in a floating-point number. We’ll assume, for our purposes, that the argument will only contain one of our six operators and two valid numbers.
prefix notation, in which the operator precedes the arguments. Using prefix notation, we would write + 2 3.
Given an array with k distinct elements, write a function to return all elements that have at least two elements greater than themselves in the same array:
#Given the following:
array = [2,3,9,7,6]#Your function should return:
This question tests our problem solving skills in Python and our understanding of the array data structure.
Since we are tasked with finding numbers that are less than at least two numbers, lets make it easy for ourselves by sorting the list. This way the last two elements will always be the two largest. Now, we can simply take every element but the last two, if the array is longer than 3 elements.
if len(array) <3:
elif len(array) == 3:
Write a function, myxml, that allows you to create simple XML output. The output from the function will always be a string. The function can be invoked in a number of ways, as shown in table 6.3.
Table 6.3 Calling myxml
Call | Return valuemyxml('foo') | <foo></foo>myxml('foo', 'bar') | <foo>bar</foo>myxml('foo', 'bar', a=1, b=2, c=3) | <foo a="1" b="2" c="3">bar</foo>
In many cases, we want to take a file in one format and save it to another format. In this function, we do a basic version of this idea. The function takes two arguments: the names of the input file (to be read from) and the output file (which will be created).
For example, if a file looks like
then the output file will be
In this exercise, you’re analyzing test data in a high school. There’s a scores directory on the filesystem containing a number of files in JSON format. Each file represents the scores for one class. Write a function, print_scores, that takes a directory name as an argument and prints a summary of the student scores it finds.
Your function should print the highest, lowest, and average test scores for each subject in each class. Given two files (9a.json and 9b.json) in the scores directory, we would see the following output:
science: min 75, max 97, average 86.4
literature: min 78, max 98, average 83.6
math: min 65, max 100, average 85.0
science: min 35, max 95, average 82.0
literature: min 38, max 98, average 72.0
math: min 38, max 100, average 77.0
The new file’s contents are the username (index 0) and the user ID (index 2). Note that a record may contain a comment, in which case it will not have anything at index 2; you should take that into consideration when writing the file. The output file should use TAB characters to separate the elements.
Thus, the input will look like this:
# I am a comment line
and the output will look like this: