Python Challenge #3 [Beginner/Iintermediate]

programmingchallenge
python

#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

Define a function for each step, printing each result in a new line, following this format:

  • Grades: …
  1. 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) #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

This challenges are just practice exercises to help those new to python, therefore they’re not that complicated; Those will (hopefully) come later… :slight_smile:

btw, did you manage to find the obfuscated message?


(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():
    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()

(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

#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.


#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


(Command-Line Ninja) #8

Man that list slicing is brief! I like it :slight_smile:


(system) #9

This topic was automatically closed after 30 days. New replies are no longer allowed.