5) c. Packing Arbitrary Arguments Lists Examples




You are traveling around the world, but your car just crashed. You need to call someone but you don’t have any helpful numbers and you have no internet access. You have found a very old computer which might contain these phone numbers. The computer is so old, that you are not able to open any file with a text editor. Assume that have some working code to read the file. Write a function, that can be called with any number of input parameters, and it prints all of them. You must hurry because your life depends on it.

1
2
3
4
5
Output:
1554981546
54894189490
19842194
51894516




You are a rogue employee, who illegally sold some company information, now close to getting discovered by your boss. You are in your boss’s office and have managed to log into his computer. You have found a file with the name of every suspicious employee who is under investigation. The file contains too many lines to check by yourself. Write a program that iterates through the file’s content and removes every line that contains your name. You are working with some of your colleagues, so you need to erase their names too. Write your function in such a way, that it works with a dynamic number of the removable names. Since you will learn File handling later, assume that you have a list containing all the names.

1
2
3
Output:
Lines without Emilien or Mariann:
['Jerry accessed the computer', 'Krulmurr is growing horns on his head']




Python allows you to pass lots of arguments to a function. But, did you know? You can even pass the “name” of a function as an argument and then invoke that function! When you do this, the new function you create is called as a “decorator”.

Decorators are simply any function that accept another function as an argument. These typically add more functionality to existing functions. Here’s an example to illustrate this:

You have built up a reputation as a programmer in your company. One day, you are kidnapped by some group who calls themselves “greatest programmers” and who are unable to solve a problem. They tell you if you cannot write a function that can call “any function they provide” then you will be killed. Write a program that takes as an input parameter a function and some arguments. Call the function with the arguments provided and solve this problem that even the “greatest programmers” weren’t able to.

1
2
3
Output:
Sum: 28
Product: 5040




In the mafia you have to hide your transactions. You get a phone call, that the FBI is on its way to check your transactions. You have 10 minutes to go through every transaction and hide your tracks. You need to create a program that iterates through your transactions and changes the delicate words to something more friendly. Don’t forget that you don’t know what kind of words do you need to change, so maybe you have to have a dynamic number of tuples as input parameters.

1
2
Output:
['3 apple for 5 USD', '2 juice for 4 USD']




Your friends are in danger and you are the only one who can help them. You can send them a message with the threat, but your country’s technical development doesn’t let you send messages with newlines in it (drat!) . You need to create a program that converts multiple messages into a single line of message. So you have to create a function, that accepts a dynamic number of input parameters and converts them into one single line of text. Hurry up, your friends’s lives depend on it.

1
2
Output:
Hurry up guys you are in danger. Don't return to your house. Get a car and leave the city.