Skip to content

Instantly share code, notes, and snippets.

'''
Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25])
and makes a new list of only the first and last elements of the given list.
For practice, write this code inside a function.
'''
import random
list_data = random.sample(range(15),8)
def new_list(list_original):
@azdafirmansyah
azdafirmansyah / Ex11CheckPrimaryNumber.py
Created November 30, 2017 07:44
Check Prime Number
'''
Exercise URL : http://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html
Ask the user for a number and determine whether the number is prime or not.
(For those who have forgotten, a prime number is a number that has no divisors.)
'''
def check_primary(num):
result = True
for j in range(2,num):
if num % j == 0:
@azdafirmansyah
azdafirmansyah / Ex10ListOverlapComprehensif.py
Created November 30, 2017 06:38
List Overlap Comprehensions
'''
Exercise URL : http://www.practicepython.org/exercise/2014/04/10/10-list-overlap-comprehensions.html
This week’s exercise is going to be revisiting an old exercise (see Exercise 5), except require the solution in a different way.
Take two lists, say for example these two:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Write this in one line of Python using at least one list comprehension. (Hint: Remember list comprehensions from Exercise 7).
@azdafirmansyah
azdafirmansyah / Ex9GuesGame.py
Created November 30, 2017 04:58
Guessing Game One
'''
Exercie URL http://www.practicepython.org/exercise/2014/04/02/09-guessing-game-one.html
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right.
Extras:
Keep the game going until the user types “exit”
Keep track of how many guesses the user has taken, and when the game ends, print this out.
'''
@azdafirmansyah
azdafirmansyah / Ex7ListComprehension.py
Last active November 30, 2017 06:16
List Comprehension
#Exercise URL : http://www.practicepython.org/exercise/2014/03/19/07-list-comprehensions.html
'''
Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100].
Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
'''
list_data = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
list_even = [x for x in list_data if x%2 == 0]
list_odd = [x for x in list_data if x%2 != 0]
print(list_even)
print(list_odd)
@azdafirmansyah
azdafirmansyah / Ex6StringList.py
Last active November 30, 2017 06:16
String List Reverse
#Exercise URL : http://www.practicepython.org/exercise/2014/03/12/06-string-lists.html
'''
Ask the user for a string and print out whether this string is a palindrome or not.
(A palindrome is a string that reads the same forwards and backwards.)
'''
data = str(input("Input your word : \n > "))
temp = ""
for i in range(len(data)):
temp += data[(len(data)-1)-i]
@azdafirmansyah
azdafirmansyah / Ex5ListOverlap.py
Last active November 30, 2017 06:17
List Overlap
#Exercise URL : http://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html
'''
Take two lists, say for example these two:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.
Extras:
@azdafirmansyah
azdafirmansyah / Ex4Divisor.py
Last active November 30, 2017 06:18
Divisors
# exercise URL : http://www.practicepython.org/exercise/2014/02/26/04-divisors.html
'''
Create a program that asks the user for a number and then prints out a list of all the divisors of that number.
(If you don’t know what a divisor is, it is a number that divides evenly into another number. For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)
'''
input_data = int(input("Input number : "))
x = []
for i in range(1,100):
if i % input_data == 0:
@azdafirmansyah
azdafirmansyah / Ex3List.py
Last active November 30, 2017 06:18
List Less Than Ten
# Exercise URL : http://www.practicepython.org/exercise/2014/02/15/03-list-less-than-ten.html
'''
Take a list, say for example this one:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
and write a program that prints out all the elements of the list that are less than 5.
Extras:
Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list in it and print out this new list.
@azdafirmansyah
azdafirmansyah / Ex8RockPaperScissor.py
Created November 30, 2017 03:00
Rock Paper Scissors Exercise
#1 = Rock
#2 = Scissors
#3 = Paper
# Exercise URL : http://www.practicepython.org/exercise/2014/03/26/08-rock-paper-scissors.html
def CheckInput(user_input):
if user_input < 1 or user_input > 3:
print("Only allow 1,2 or 3 number\nLet's Try Again\n----------------------\n")
return False
else: