Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
codeacademy python tasks
#python 2.7.12 def censor(text, word): wordList = text.split(' ') for index, item in enumerate(wordList): if item == word: wordList[index] = "*"*len(word) return " ".join(wordList) print '--censor task--' print censor('kwa kwa raz kwa', 'raz') def count(sequence, item): result = 0 for i in sequence: if i == item: result += 1 return result print '--count task--' print count([2,3,6.5,6.54,3,2,7,8,9,8,7,6,5,4,4,43,3,3,2,2,2], 2) def purify(items): result = [i for i in items if i % 2 == 0] return result print '--purify task--' print purify([1,5,6,4,2,3,6,8,9,8,7,6]) def product(items): result = 1 for i in items: result *= i return result print '--product task--' print product([1,2,2,3,4,5]) def check_val(items, val): for i in items: if i == val: return True return False def remove_duplicates(items): result = [] for i in items: if check_val(result, i): continue else: result.append(i) #result = [i for i in items if i not in result] #for i in items: # if i not in result: # result.append(i) return result print '--remove_duplicates task--' print remove_duplicates([1,2,3,0,3,0,3,2,4,5,6,6,6,100,0,44]) def median(items): if(len(items) == 1): return 1 newArr = sorted(items) if len(newArr) % 2 == 0: temp = newArr[len(newArr)/2-1 : len(newArr)/2+1] return sum(temp) / 2.0 else: temp = newArr[len(newArr)/2 : len(newArr)/2+1] return temp[0] print '--median task--' print median([1,2.3,3,3]) # # grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] def print_grades(grades_input): for grade in grades_input: print grade def grades_sum(scores): total = 0 for score in scores: total += score return total def grades_average(grades_input): sum_of_grades = grades_sum(grades_input) average = sum_of_grades / float(len(grades_input)) return average def grades_variance(scores): average = grades_average(scores) variance = 0 for score in scores: variance += (average - score) ** 2 variance /= len(scores) return variance def grades_std_deviation(variance): return variance ** 0.5 variance = grades_variance(grades) print "--EXAM STATISTICS task--" print_grades(grades) print "sum of grades:", grades_sum(grades) print "average grade:", grades_average(grades) print "variance grade:", grades_variance(grades) print "standart deviation:", grades_std_deviation(variance) # binary representation of numbers one = 0b1 two = 0b10 three = 0b11 four = 0b100 five = 0b101 six = 0b110 seven = 0b111 eight = 0b1000 nine = 0b1001 ten = 0b1010 eleven = 0b1011 twelve = 0b1100 def check_bit4(iput): mask = 0b1000 print i if iput & mask: return 'on' else: return 'off' print check_bit4(0b1110)
run
|
edit
|
history
|
help
0
Type lala suthar
a
First programm
rstring
fb_series
gameboy
NormalizeTags
my shopping list at supermarket
Simple code to find all divisors for a number
bacs_class