+ 4

Apple Of My Eye task. Searching for easier way

Hello Guys I was able to solve the task in list operators from Python Data Structure, but my solution looks very complicated. Maybe you can help me to find an easier way to solve it Task: List Operations You are given a 2D matrix, which represents the number of people in a room, grouped by their eye color and gender. The first row represents the male gender, while the second row represents females. The columns are the eye colors, in the following order: brown, blue, green, black data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] PY The data shows that, for example, there are 20 green eyed females in the room (2nd row, 3rd column). Our program needs to take eye color as input and output the percentage of people with that eye color in the room. Sample Input: blue Sample Output: 36 Explanation: There are 11+32=43 people with blue eyes, which is 36% of the total. My Solution: data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] #Sum of People in Room People = int(sum(data[0]+data[1])) # print(int(People)) color = input() #working with if operator and using the formular to count the percentage if color == 'brown': print(int( int(data[0][0]+data[1][0])/People*100 )) elif color == 'blue': print(int( int(data[0][1]+data[1][1])/People*100 )) elif color == 'green': print(int( int(data[0][2]+data[1][2])/People*100 )) elif color == 'black': print(int( int(data[0][3]+data[1][3])/People*100 ))

22nd May 2021, 11:30 AM
Eduard Rein
Eduard Rein - avatar
10 Answers
+ 7
You can use dictionary color = input() data = { 'brown' : 31, 'blue' : 43, 'green' : 25, 'black' : 19 } print(int(data[color]/118*100))
22nd May 2021, 1:13 PM
Simba
Simba - avatar
+ 4
Because the exercise is for list, I don’t think its in the spirit of the exercise to use dictionaries. You could shorten your code by putting the acceptable inputs into a list. Then, use a for loop to iterate through the list. When a match is found with the input value, sum the male and female values and print the average. Here’s an example: data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here colors = ["brown","blue","green","black"] total = sum(data[0]) + sum(data[1]) for x in range(0,len(colors)): if color == colors[x]: avg = int( (data[0][x]+data[1][x])/total*100) print(avg)
26th Jul 2021, 1:09 PM
Jeremy White
Jeremy White - avatar
+ 3
don't write more just easy like this ✔✔✔👍👍👍👍✔✔✔ data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here if color == "brown": print(int((data[0][0] + data[1][0]) * 100 / 118)) elif color == "blue": print(int((data[0][1] + data[1][1]) * 100 / 118)) elif color == "green": print(int((data[0][2] + data[1][2]) * 100 / 118)) elif color == "black": print(int((data[0][3] + data[1][3]) * 100 / 118)) else: print(None)
9th Aug 2021, 4:59 PM
ANDREW TSEGAYE
ANDREW TSEGAYE - avatar
+ 1
exercise require to apply lists Not Dictionary ?
2nd Jul 2021, 8:08 PM
JRAMAHES
JRAMAHES - avatar
+ 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() total = 0 for row in data: total += sum(row) colors = ['brown', 'blue', 'green', 'black'] x = colors.index(color) chosen_people = data[0][x] + data[1][x] print(int(chosen_people*100 / total))
4th Feb 2022, 7:38 AM
Eugenie
+ 1
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here for i in data: suma+= sum(i) # suma = sum(data[0]) + sum(data[1]) if color == 'brown': print(int((data[0][0] + data[1][0])*100/suma)) elif color == 'blue': print(int((data[0][1] + data[1][1])*100/suma)) elif color == 'green': print(int((data[0][2] + data[1][2])*100/suma)) elif color == 'black': print(int((data[0][3] + data[1][3])*100/suma))
26th Feb 2022, 1:57 AM
Liria
Liria - avatar
0
Sorry for the long post ))) here is a Potato or something :D Any 9gagers here btw?
22nd May 2021, 11:33 AM
Eduard Rein
Eduard Rein - avatar
0
My other solution import math d = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() cd = ["brown","blue","green","black"] c = cd.index(color) total = d[0][c]+d[1][c] cin = (total/120)*100 print(math.ceil(cin))
15th Dec 2021, 1:41 AM
Samnith Vath
Samnith Vath - avatar
0
import math data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] color = input() #your code goes here if color == "brown": x = data[0][0] + data[1][0] p = int((x/118)*100) print(math.floor(p)) elif color == "blue": x = data[0][1] + data[1][1] p = int((x/118)*100) print(math.floor(p)) elif color == "green": x = data[0][2] + data[1][3] p = int((x/118)*100) print(math.floor(p)) elif color == "black": x = data[0][3] + data[1][3] p = int((x/118)*100) print(math.floor(p))
11th Nov 2022, 12:04 PM
Kainat Raisa
Kainat Raisa - avatar
0
data = [ [23, 11, 5, 14], [8, 32, 20, 5] ] colordict = { "brown": 0, "blue": 1, "green": 2, "black": 3 } color = input() #your code goes here totNrPeo = (sum(data[0]))+(sum(data[1])) colorNrPeo = (data[0][(colordict[color])])+(data[1][(colordict[color])]) print (round(colorNrPeo/totNrPeo*100))
8th Dec 2022, 9:48 PM
Laurent