-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week4-List-Quiz.py
103 lines (81 loc) · 4.44 KB
/
Week4-List-Quiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
###Given a list of filenames, we want to rename all the files with extension hpp to the extension h. To do this, we
# would like to generate a new list called newfilenames, consisting of the new filenames. Fill in the blanks in the
# code using any of the methods you’ve learned thus far, like a for loop or a list comprehension.
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
newfilenames = []
for filename in filenames:
if filename.endswith("hpp"):
x = filename.replace("hpp","h")
newfilenames.append(x)
else:
newfilenames.append(filename)
print(newfilenames)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]
###Let's create a function that turns text into pig latin: a simple text transformation that modifies each word moving
# the first character to the end and appending "ay" to the end. For example, python ends up as ythonpay.
def pig_latin(text):
say = []
# Separate the text into words
words = text.split()
for word in words:
# Create the pig latin word and add it to the list
word = word[1:] + word[0] + "ay"
say.append(word)
# Turn the list back into a phrase
return (" ".join(say))
print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
###The permissions of a file in a Linux system are split into three sets of three permissions: read, write, and execute
# for the owner, group, and others. Each of the three values can be expressed as an octal number summing each permission,
# with 4 corresponding to read, 2 to write, and 1 to execute. Or it can be written with a string using the letters r, w,
# and x or - when the permission is not granted. For example: 640 is read/write for the owner, read for the group, and no
# permissions for the others; converted to a string, it would be: "rw-r-----" 755 is read/write/execute for the owner, and
# read/execute for group and others; converted to a string, it would be: "rwxr-xr-x" Fill in the blanks to make the code
# convert a permission in octal format into a string format.
def octal_to_string(octal):
result = ""
value_letters = [(4, "r"), (2, "w"), (1, "x")]
# Iterate over each of the digits in octal
for x in [int(n) for n in str(octal)]:
# Check for each of the permissions values
for value, letter in value_letters:
if x >= value:
result += letter
x -= value
else:
result += "-"
return result
print(octal_to_string(755)) # Should be rwxr-xr-x
print(octal_to_string(644)) # Should be rw-r--r--
print(octal_to_string(750)) # Should be rwxr-x---
print(octal_to_string(600)) # Should be rw-------
###The group_list function accepts a group name and a list of members, and returns a string with the format: group_name:
#member1, member2, … For example, group_list("g", ["a","b","c"]) returns "g: a, b, c". Fill in the gaps in this function
#to do that.
def group_list(group, users):
members = group+": "+", ".join(users)
return members
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
###The guest_list function reads in a list of tuples with the name, age, and profession of each party guest, and prints
# the sentence "Guest is X years old and works as __." for each one. For example, guest_list(('Ken', 30, "Chef"),
# ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")) should print out: Ken is 30 years old and works as Chef.
# Pat is 35 years old and works as Lawyer. Amanda is 25 years old and works as Engineer.
# Fill in the gaps in this function to do that.
def guest_list(guests):
#print(type(guests))
for guest in guests:
# print(guest)
# print(guest[0])
print("{} is {} years old and works as {}".format(guest[0],guest[1],guest[2]))
guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])
#Click Run to submit code
"""
Output should match:
Ken is 30 years old and works as Chef
Pat is 35 years old and works as Lawyer
Amanda is 25 years old and works as Engineer
"""