Giter Club home page Giter Club logo

python-code's People

Watchers

 avatar  avatar

python-code's Issues

A dreadful problem for recursive function: SLOW !

The function "fac" uses recursive method in defining function to calculate the factorial of an integer. The function "Fac" uses a for loop in doing the same thing. We want to compare the time these two methods and another function "math.factorial" need to spend for doing the same thing. Here is the code.

import math
def fac(n):
    n = round(n)
    if n == 0:
        return 1
    else:
        return n*fac(n-1)

def Fac(n):
    n = round(n)
    if n == 0:
        return 1
    else:
        sum = 1
        for i in range(1,n+1):
            sum *= i
        return sum


def runtime(s):
    import time
    time1 = time.clock()
    fac(s)
    time2 = time.clock()
    Fac(s)
    time3 = time.clock()
    math.factorial(s)
    time4 = time.clock()   

    x1 = time2 - time1
    x2 = time3 - time2
    x3 = time4 - time3
    print('The time function "fac" spent was: ', x1, 'seconds.')
    print('The time function "Fac" spent was: ', x2, 'seconds.')
    print('The time function "math.factorial" spent was: ', x3, 'seconds.')

Now we can run the function "runtime" with arbitrary positive integer we want to test.

>>> runtime(5)
The time function "fac" spent was:  1.1999999999900979e-05 seconds.
The time function "Fac" spent was:  8.000000000230045e-06 seconds.
The time function "math.factorial" spent was:  3.000000000419334e-06 seconds.
>>> runtime(10)

The time function "fac" spent was:  1.4000000000180535e-05 seconds.
The time function "Fac" spent was:  9.000000000369823e-06 seconds.
The time function "math.factorial" spent was:  2.9999999995311555e-06 seconds.
>>> runtime(20)

The time function "fac" spent was:  2.3000000000550358e-05 seconds.
The time function "Fac" spent was:  9.999999999621423e-06 seconds.
The time function "math.factorial" spent was:  3.000000000419334e-06 seconds.
>>> runtime(50)

The time function "fac" spent was:  4.2000000000541604e-05 seconds.
The time function "Fac" spent was:  1.4999999999432134e-05 seconds.
The time function "math.factorial" spent was:  5.999999999950489e-06 seconds.

We can see that the recursive method is even much slower than even the notorious looping for its slowness. The function "math.factorial" provided by Python itself is quickest. So recursive method should be avoided if we have better ways of coding.

Count letter frequencies in a string

The following function takes a string as input and calculate the frequency of each letter, the print the freq table.

def most_frequent(S):
    S.lower()
    dic = dict()    # create an empty dictionary
    for i in S:
        dic[i] = 1 + dic.get(i, 0) # fill up the dictionary with letters and their freq
    t = []
    for i in dic:
        t.append((dic[i], i))   # create a tuple from the dictionary
    res = sorted(t, reverse = True)     # sort the tuple
    for x, y in res:
        print(y, x)

Some texts in English are selected and run the function most_frequent.

data = open("word.txt")
x = data.read()
print(most_frequent(x))

The result is as follows, the first several lines are the same with wikipedia record.

 7959
e 4958
t 3581
a 2892
o 2884
i 2810
n 2609
s 2500
r 2271
h 1944
l 1698
d 1270
c 1262
u 1105
m 927
p 829
y 764
f 711
g 674
w 635
b 602
v 457
, 404
. 356
k 273

 236
’ 154
T 97
A 94
I 92
S 90
x 87
- 74
E 62
j 54
W 53
B 46
– 44
‘ 41
q 40
: 40
C 38
0 37
L 34
1 32
? 31
R 30
P 30
O 29
M 29
H 29
D 29
N 26
; 26
' 24
F 23
2 21
) 21
( 21
G 19
9 19
Y 17
U 17
K 15
z 14
7 11
J 10
V 9
6 9
8 6
5 6
4 6
… 4
] 4
[ 4
& 4
! 4
” 3
“ 3
Z 3
é 2
3 2
— 1
Q 1
% 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.