Home > Mobile >  It took me 45 minutes to answer this python list question. What is a simpler method?
It took me 45 minutes to answer this python list question. What is a simpler method?

Time:04-26

So I've been really struggling with learning python. It's been killing me. It's been about 2 months now. I have 2 questions. 1. What is a simpler way to solve this problem that doesn't take 45 minutes? And 2 - how long did you have to practice Python before you became competent?

When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This adjustment can be done by normalizing to values between 0 and 1, or throwing away outliers.

For this program, read in a list of floats and adjust their values by dividing all values by the largest value. Output each value with two digits after the decimal point. Follow each number output by a space.

Ex: If the input is:

30.0 50.0 10.0 100.0 65.0

the output is:

0.30 0.50 0.10 1.00 0.65

''' Type your code here. '''
num = input()
makeList = num.split()
biggest = float(makeList[0])
for index, value in enumerate(makeList):
    if float(value) > float(biggest):
        biggest = value
lastString = ""
for i in makeList:
    answer = ("{:.2f}".format(float(i)/float(biggest)))
    lastString  = answer   " "
print(lastString)

1: Compare output 3 / 3

Input
30.0 50.0 10.0 100.0 65.0
Your output

2: Compare output 4 / 4

Input
5.0 10.0 15.0 20.0 25.0 30.0 35.0
Your output
0.14 0.29 0.43 0.57 0.71 0.86 1.00 

3: Compare output 3 / 3

Input
99.9 52.5 12.6 200.0
Your output
0.50 0.26 0.06 1.00 
0.30 0.50 0.10 1.00 0.65 

CodePudding user response:

You just need to do this step by step. Grab the input:

num = input()

Split it into words and convert them all to floats:

vals = [float(k) for k in num.split()]

Now we have a list of floats. Find the largest:

maxval = max(vals)

Divide them all by that value:

for i in range(len(vals)):
    vals[i] /= maxval

Print them:

for i in vals:
    print( "{:.2f}".format(i), end=' ')
print()

CodePudding user response:

Answering your questions:

  1. What is a simpler way to solve this problem?

Step 1: What you can do is get the data as "float" with skipping first number

Alternatively, your way of looping with "input" works too if the requirement is sending lines one by one

data = list(map(float, text.strip().split('\n')))[1:]

Step 2: Now calculate the maximum value

maximum = max(data)

Step 3: Finally, just divide each element in list by the maximum you found in step 2 using a list comprehension

You can learn about list comprehension here: https://www.w3schools.com/python/python_lists_comprehension.asp

out = [e/maximum for e in data]
  1. How long did you have to practice Python before you became competent?

Well, there's no fixed answer for that, focus on solving problems and you are good to go. Also, there's no defined time that can quantify how "long" you need to practice. As far as I believe, you practice as long as you want to be in this field!

CodePudding user response:

A simpler way would be this:

inpt = "30.0 50.0 10.0 100.0 65.0" # take the input together
inpt = [float(val) for val in inpt.split()] # turn the input into a list of floats
large = max(inpt) # find the largest element

# print each element divided by large and specify 2 decimal places as the format
print(*(f"{(val/large):.2f}" for val in inpt))

The code prints precisely: 0.30 0.50 0.10 1.00 0.65

CodePudding user response:

A simpler way would in pseudocode. And pseudocode and planning before you code really does help if your tackling a big problem. When I was a beginner I used to think it's all crap to pseudocode and instead go straight into coding.

First get the input

mylist = input()

Create a new list/output

newlist = []

Get the max

maxval = max(mylist)

Go through the old list and divide by the max and at each step add to the new list

for i in mylist:

  x = i / maxval

  newlist.append(x)

As for your second question, programming takes a lot of patience to get good at. You must continually struggle and struggle through problems to develop the skills. Although of course having a good teacher or resources will save you from a lot of needless struggling. Some people seem faster than others but the way I see it, they just have more experience working their mind in a logical way.

I only had to practice Python for a few months to get a hang of it because I learned C first. And I would suggest starting with a low-level language before going to higher-level ones but this is a whole different topic. It's 2022 you can learn anything by yourself but I'm old-fashioned I'd recommend if you really want to do this and make a living out of it go to college (very debatable I know).

CodePudding user response:

The simplest way to do this problem would be to first convert this into a numpy array and then divide the whole array by the largest number,

import numpy

test = np.array([30.0, 50.0, 10.0, 100.0, 65.0])
print(test/test.max())

And about the second question...all I can say is that don't worry too much. Learning anything is pretty difficult at first, everyone was once at the same stage as you are right now. Have a systematic plan that you follow each day, know what you should be studying right now, and what you should be studying after you are done with the current topic. Seek someone's guidance if you feel like it. Don't take too much stress because not everyone learns at the same rate. It's okay to take a little more time...all that matters is that you learnt something.

From my experience it took me about 2-3 months to get Intermediate level at Python but that was because I paid for a course which provided me a systematic plan and good mentorship. But I am damn sure that you can level up even without spending a penny. So cheer up!

  • Related