n3xUs
June 24, 2016, 9:36pm
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]
:
print
the grades
print
the total sum of the grades
print
& calculate the average
print
& calculate the variance
print
& calculate the standard deviation
Def
ine a function for each step, printing each result in a new line, following this format:
Grades Sum: …
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
oaktree
(oaktree)
June 25, 2016, 3:09am
2
Not much to it:
grades = [70, 95, 30, 65, 100, 85, 55, 90, 95, 90, 45, 50.5]
# 1. print out grades
sum = 0
print "Grades: "
grades.each do |g|
sum += g
print "#{g} "
end
# 2. sum
print "\nGrades Sum: #{sum}\n"
# 3. average
mean = sum / grades.length
print "Average Grade: #{mean}\n"
# 4. variance
print "Variance: "
dev_squares = []
grades.each do |g|
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
n3xUs
June 25, 2016, 12:31pm
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
(Nukepruf)
June 26, 2016, 4:18am
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():
print(grades)
gsum = sum(grades)
print("Grades sum:",gsum)
average = gsum / len(grades)
print("Grades Sum:",format(average,'.2f'))
variance=grades_variance(grades,average)
print("Grade Variance: ",format(variance,'.2f'))
deviation = square_root(variance)
print("Grade Deviation:",format(deviation,'.2f'))
def grades_variance(grades, average):
variance = 0
for i in range(0, len(grades)):
variance += (average - grades[i]) ** 2
return variance / len(grades)
def square_root(vari):
return vari**(1/2.0)
main()
1 Like
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
n3xUs
June 26, 2016, 1:00pm
6
Hey @Nukepruf , I think your code needs some work, but first, could you format it correctly?
If you need some help doing so, follow this link .
n3xUs
June 26, 2016, 1:09pm
7
My solution:
grades = [70, 95, 30, 65, 100, 85, 55, 90, 95, 90, 45, 50.5]
def grades_sum(grades):
total = 0
for grade in grades:
total += grade
return total
def grades_average(grades):
average = grades_sum(grades) / float(len(grades))
return average
def grades_variance(scores):
average = grades_average(scores)
variance = 0
for score in scores:
variance += (average - score) ** 2
n = variance / float(len(scores))
return n
def grades_std_deviation(variance):
return variance ** 0.5
variance = grades_variance(grades)
print 'Grades : ', grades
print 'Grades Sum: ', grades_sum(grades)
print 'Grades Average: ', grades_average(grades)
print 'Grades Variance: ', grades_variance(grades)
print 'Grades Std Deviation: ', grades_std_deviation(variance)
#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
pry0cc
(Leader & Offsec Engineer & Forum Daddy)
June 27, 2016, 8:03am
8
Man that list slicing is brief! I like it
1 Like
system
(system)
Closed
January 21, 2018, 12:31am
9
This topic was automatically closed after 30 days. New replies are no longer allowed.