Homework:
Let’s say you have three shirts and four pants. Print every possible combination using a list for the shirts and a list for the pants. (Hint: list comprehension.)
“Brian, the officer in charge; Mark, the chef; Dexter, my dog; and Petunia, my cat”.
Split this string only by the first two semicolons.
Find the index of the first t, the first x, and the first z.
Resources:
Class slides: https://docs.google.com/presentation/d/1ZTJES07Y4f7P5JrHNQz-2pIJH0nmTCpvm5kHVGd-PXQ/edit?usp=sharing
Class code: https://replit.com/join/xfdqkiwkwr-shravyas
Completed game: https://replit.com/join/akmhvbfxsh-shravyas
Contact Info:
shravyassathish@gmail.com (mentor)
kingericwu@gmail.com (TA)
felixguo41@gmail.com (TA)
shirts = ["checkered", "striped", "t"]
pants = ["pyjama", "long", "short", "sweat"]
combinations = [i+ " shirt and " + j + " pants" for i in shirts for j in pants]
print(f"Part 1: for reference, shirts are: {shirts} and pants are: {pants}\n\nPossible combinations are:{combinations}\n\n")
sentence = "Brian, the officer in charge; Mark, the chef; Dexter, my dog; and Petunia, my cat"
print("Part 2: a. the split string is " + str(sentence.split(";", 2)) + ". b. The first t is located at index " + str(sentence.index("t")) + ", the first x at index " + str(sentence.index("x")) + ", and there is no z in the sentence.")
shirts = ["shirt 1", "shirt 2", "shirt 3"] pants = ["pants 1", "pants 2", "pants 3", "pants 4
"] for i in shirts: for b in pants: print(f"outfit {i}, {b}") 2. str = "Brian, the officer in charge; Mark, the chef; Dexter, my dog; and Petunia, my cat" x = str.split("; ", 2) for substr in x: print(f"index of first t in '{substr}' is {substr.find('t')}") print(f"index of first x in '{substr}' is {substr.find('x')}") print(f"index of first z in '{substr}' is {substr.find('z')}\n")
Like Reply
1.
shirts = ["shirt red", "shirt blue", "shirt yellow"]
pants = ["pants blue", "pants yellow", "pants red", "pants green"]
for i in shirts:
for b in pants:
print(f"outfit {i}, {b}")
2.
str = "Brian, the officer in charge; Mark, the chef; Dexter, my dog; and Petunia, my cat"
x = str.split("; ", 2)
for substr in x:
print(f"index of first t in '{substr}' is {substr.find('t')}")
print(f"index of first x in '{substr}' is {substr.find('x')}")
print(f"index of first z in '{substr}' is {substr.find('z')}\n")
Solution:
1.
shirts=["shirt 1", "shirt 2", "shirt 3"]
pants=["pants 1", "pants 2", "pants 3"]
for a in range(3):
for b in range(4):
combo=[shirts[a],pants[b]]
print(" ".join(combo))
2.
str="Brian, the officer in charge; Mark, the chef; Dexter, my dog; and Petunia, my cat"
x=[]
for char in str:
if char != ";":
x.append(char)
else:
break
str2 = "".join(x)
x=[]
z=0
for char in str:
if char != ";":
x.append(char)
else:
z+=1
if z != 2:
x.clear()
elif z==2:
break
str3="".join(x)
print(str2,str3)
a=-1
x=0
t=0
z=0
for char in str:
a+=1
if char == "t" and t==0:
print("t",a)
t+=1
elif char == "x" and x ==0:
x+=1
print("x",a)
elif char == "z" and z==0:
print("z",a)
z+=1