 Python Challenge #3 [Beginner/Iintermediate]

#1

#Exam Statistics#

This challenges are based of challenges in Codecademy’s python exercises.

This challenge is fairly simple and straight forward. Given the list `grades = [70, 95, 30, 65, 100, 85, 55, 90, 95, 90, 45, 50.5]`:

1. `print` the grades

2. `print` the total sum of the grades

3. `print` & calculate the average

4. `print` & calculate the variance

5. `print` & calculate the standard deviation

`Def`ine a function for each step, printing each result in a new line, following this format:

etc…

I’ll post my code soon.

Happy Coding!

EXTRA CHALLENGE

Given the string `my_string = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"`, find the obfuscated message using two methods: first use list slicing and then lamba expressions

0 Likes

(oaktree) #2

Not much to it:

``````grades = [70, 95, 30, 65, 100, 85, 55, 90, 95, 90, 45, 50.5]

sum = 0
sum += g
print "#{g} "
end

# 2. sum

# 3. average

# 4. variance
print "Variance: "
dev_squares = []
print "#{(g - mean).abs} "
dev_squares << ((g-mean) ** 2)
end

# 5. standard deviation
mean_sqr = 0
dev_squares.each do |d|
mean_sqr += d
end
mean_sqr /= dev_squares.length
mean_sqr **= 0.5
print "\nStandard Deviation: #{mean_sqr}\n"
``````

Deobfuscate

Here ya go, @n3xUs:

``````my_string = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
print "#{my_string.gsub('X','').reverse}\n"
``````
3 Likes

#3

This challenges are just practice exercises to help those new to python, therefore they’re not that complicated; Those will (hopefully) come later… btw, did you manage to find the obfuscated message?

1 Like

(Nukepruf) #4

This is my python3 solution. I’m quite new to this so I enjoyed this challenge a lot!

``````grades =[70, 95, 30, 65, 100, 85, 55, 90, 95, 90, 45, 50.5]
def main():

deviation = square_root(variance)

variance = 0
variance += (average - grades[i]) ** 2

def square_root(vari):
return vari**(1/2.0)

main()
``````
1 Like

(0x00Jinx) #5

Since there is already a solution for the main challenge in python, here is a solution for the extra challenge in python:

``````my_string = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = "".join([x for x in my_string if x != "X"])[::-1]
print message
``````
2 Likes

#6

Hey @Nukepruf, I think your code needs some work, but first, could you format it correctly?

0 Likes

#7

My solution:

``````grades = [70, 95, 30, 65, 100, 85, 55, 90, 95, 90, 45, 50.5]

total = 0

return average

variance = 0
for score in scores:
variance += (average - score) ** 2
n = variance / float(len(scores))
return n

return variance ** 0.5

``````
``````#USING LIST SLICING
my_string = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = [::-1]
message = [::2]
print message

#USING LAMBA EXPRESSIONS
my_string = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
message = my_string[::-1]
message = filter(lambda x: x != 'X', message)
print message

``````
2 Likes

Man that list slicing is brief! I like it 