Showing posts with label edx. Show all posts
Showing posts with label edx. Show all posts

Wednesday, June 1, 2016

Newton-Rapson Finding Root using Python

Newton-Rapson tuuh... metode kyk bisection juga, cuman ini konsepnya pake polinomialnya newton... Klu di implementasi di python buat nyari akar suatu variabel float, tampilannya kyk gini :


1.
2.
3.
4.
5.
6.
7.
8.
epsilon = 0.01
y = 24.0
guess = y/2.0

while abs(guess*guess - y) >= epsilon:
    guess = guess - (((guess**2) - y)/(2*guess))
    print(guess)
print('Square root of ' + str(y) + ' is about ' + str(guess))

Hasilnya :

Jawaban L3 Problem 9 - Intro to Computer Science & Programming Using Python - edx.org



Jawaban raw Hisoka kyk gini :


 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
32
33
34
35
36
import random

low = 0;
high = 100;
ans = 0;

print("Haay.. I'm Hyosoka's awesome guess number");
print("So...Please Choose number between 0 and 100....Then I'm gonna guess your secret number... :D");
print("Done...? y or n");
usr=raw_input();
while(usr == "n"):
    print("Please Choose number between 0 and 100....");
    print("Done...? y or n");
    usr=raw_input();

ans = random.randint(0,100);    
print("Is your secret number = " + str(ans));
print("");
guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");
while (guess != "c"):    
    if( guess == "h"):
        high=ans;
        ans = (low+high)/2;
    elif(guess == "l"):
        low = ans;
        "high = 100;"
        ans = (low+high)/2;
    else:
        while((guess != "h") or (guess != "l")):            
            print("Sorry... I don't understand your input");
            guess = raw_input("Please just enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low ");
            if(guess == "h" or guess == "l"): break;
    print("Is your secret number = " + str(ans));
    guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");

print("Yeeei... So your secret number is = "+str(ans) +" heheh... :D");                

Yang itu udah ditest... dan berhasil... nah pas dimasukin ke edx-nya diubah jadi kyk gini :


 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
low = 0;
high = 100;
ans = 0;

print "Please think of a number between 0 and 100!"

ans = 50;    
print("Is your secret number = " + str(ans));
print("");
guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");
while (guess != "c"):
    if( guess == "h"):
        high=ans;
        ans = (low+high)/2;
    elif(guess == "l"):
        low = ans;
        "high = 100;"
        ans = (low+high)/2;
    else:
        while(guess != "c" or guess != "h" or guess != "l"):
            print("Sorry... I don't understand your input");
            guess = raw_input("Please just enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");        
            if(guess =="h" or guess =="l"): break;
    print("Is your secret number = " + str(ans));
    guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");

print("Game over. Your secret number was: "+str(ans));    

Cuman dianggap salaah.... :v seharusnya udah bener, soalnya udah ditest di canopy... :v


Mmmm.... tapi udah ngelapor... ehehe... :D
Btw... jawaban dari edx-nya seperti ini :


 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
print("Please think of a number between 0 and 100!")

# At the start the highest the number could be is 100 and the lowest is 0.
hi = 100
lo = 0
guessed = False

# Loop until we guess it correctly
while not guessed:
    # Bisection search: guess the midpoint between our current high and low guesses
    guess = (hi + lo)/2
    print("Is your secret number " + str(guess)+ "?")
    user_inp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

    if user_inp == 'c':
        # We got it right!
        guessed = True
    elif user_inp == 'h':
        # Guess was too high. So make the current guess the highest possible guess.
        hi = guess
    elif user_inp == 'l':
        # Guess was too low. So make the current guess the lowest possible guess.
        lo = guess
    else:
        print("Sorry, I did not understand your input.")

print('Game over. Your secret number was: ' + str(guess))

Sekian dulu... :)

Bisection Search Using Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
x = 123
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
    print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
    numGuesses += 1
    if ans**2 < x:
        low = ans
    else:
        high = ans
    ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))

Hasil :

Square Approximation Method using Python

Hasil :
Di sini stepnya yg jadi kunci, terlalu kecil berarti teliti, cuman perhitungannya lebih lama,,, terlalu besar cepet, cuman kurang teliti atau malah g' ada jawabanya didapat dari method approximation ini.. :D

Bermain dengan Float.. :D

Contoh representasi float dalam decimal :
3/8 = 0.375 = 3*10**(-1) + 7*10**(-2) + 5*10**(-3)

 Contoh program dalam python untuk ngekonvert float ke binary :


Hasilnya :

Waaaaww... binary dari 0.1 kereeun.. :v

Decimal to Binary in Python


Wednesday, March 30, 2016

Jawaban L3 Problem 8 - Intro to Computer Science & Programming Using Python - edx.org





Jawaban L3 Problem 7 - Intro to Computer Science & Programming Using Python - edx.org





Jawaban L3 Problem 6 - Intro to Computer Science & Programming Using Python - edx.org






Jawaban L3 Problem 5 - Intro to Computer Science & Programming Using Python - edx.org


 Jawaban dari edx-nya :






 Jawaban edx :

Jawaban L3 Problem 4 - Intro to Computer Science & Programming Using Python - edx.org

Berikut jawaban problem yang ke-4




mmmm.... itu yang "hola" salah... :v, jawaban yang bener ternyata h, o, l, a....brrr... lupa naroin koma koma... :v

Jawaban L3 Problem 3 - Intro to Computer Science & Programming Using Python - edx.org

Berikut jawaban untuk problem ketiga lecture 3
 






Jawaban L3 Problem 2 - Intro to Computer Science & Programming Using Python - edx.org

Berikut jawaban untuk problem 2a

Jawaban dari edx-nya :

=================================================================
Penjelasan dari edx-nya :
==============================================================


Penjelasan dari edx