-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy patha1.py
33 lines (22 loc) · 801 Bytes
/
a1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Code listing #10
""" Module A (a.py) - Implement functions that operate on series of numbers """
# Note this is version 1 of a.py so called a1.py
def squares(narray):
""" Return array of squares of numbers """
return pow_n(array, 2)
def cubes(narray):
""" Return array of cubes of numbers """
return pow_n(narray, 3)
def pow_n(narray, n):
""" Return array of numbers raised to arbitrary power n each """
return [pow(x, n) for x in narray]
def frequency(string, word):
""" Find the frequency of occurrences of word in string
as percentage """
word_l = word.lower()
string_l = string.lower()
# Words in string
words = string_l.split()
count = words.count(word_l)
# Return frequency as percentage
return 100.0*count/len(words)