-
-
Save cibofdevs/42c3871019e0adc7d63b4b1ba8e1b2b9 to your computer and use it in GitHub Desktop.
| # rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) with every month separated by a comma. | |
| # Write code to compute the number of months that have more than 3 inches of rainfall. | |
| # Store the result in the variable num_rainy_months. In other words, count the number of items with values > 3.0. | |
| # Hard-coded answers will receive no credit. | |
| rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85" | |
| rainfall_mi_split = rainfall_mi.split(",") | |
| num_rainy_months = 0 | |
| for x in rainfall_mi_split: | |
| x = float(x) | |
| if x > 3.0: | |
| num_rainy_months += 1 | |
| print(num_rainy_months) |
Using list comprehension or creating generator (depending on the size of the iterable) to convert string elements into the float, and then using accumulator pattern to count the number of months that have more than 3 inches of rainfall.
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
values_str = rainfall_mi.split(',')
values_float = [float(value) for value in values_str] # values_float = (float(value) for value in values_str) # creating a generator
num_rainy_months = 0
for value in values_float:
if value > 3.0:
num_rainy_months += 1
print('num_rainy_months', num_rainy_months)But probably the shortest solution to the problem is
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months = sum(1 for x in rainfall_mi.split(",") if float(x) > 3.0)
print('num_rainy_months', num_rainy_months)rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
rainfall_mi_split = rainfall_mi.split(",")
num_rainy_months = 0
for i in rainfall_mi_split:
if float(i) > 3.0:
num_rainy_months += 1
print(num_rainy_months)
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
rainfall= rainfall_mi.split(",")
num_rainy_months=0
for i in rainfall:
i = float(i)
if i > 3.0:
num_rainy_months += 1
print(num_rainy_months)
sentence = "students flock to the arb for a variety of outdoor activities such as jogging and picnicking"
code:
sentence1 = sentence.split() #converting the string into list
same_letter_count = 0
for i in sentence1 :
if i[0] == i[-1]: #setting the conditional for each element of list i.e. first element should be equal to last one !
same_letter_count = same_letter_count + 1
print(same_letter_count )
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
num_rainy_months = 0
rainfall_mi = rainfall_mi.split(",")
for i in rainfall_mi:
if float(i) > 3.0:
num_rainy_months += 1
print(num_rainy_months)
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
rain=rainfall_mi.split(",")
num=[]
for i in rain:
if float(i)>3.0:
num.append(i)
num_rainy_months=len(num)
print(num_rainy_months)