Tuesday 27 September 2016

Program for Rock Paper Scissor Game in Python

This is the 8th exercise of practicepython.

Exercise:

Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
Remember the rules:
  • Rock beats scissors
  • Scissors beats paper
  • Paper beats rock
My Solution is:






Output 1:





Output 2:





GitHub Reference:

https://gist.github.com/pinkpretty/e648c67ba012fc09d0fb963b0a249acc


Happy Coding ! :)

Tuesday 20 September 2016

Program to find the even elements in List - Python

This is the practice python 7th exercise.

Exercise:

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.


Solution;


The solution is simple if you use list comprehension.


 


Output:





GitHub reference:


Github code:


https://gist.github.com/pinkpretty/48c8e0a700017c54d3352ca12d4117f5


Happpy coding ! :)

Program for String Palindrome in python

This is the 6th Exercise of practicepython.

Exercise

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.)


Solution:


String Palindrome in python can be done with [::-1].\


str(n) == str(n)[::-1]


[::-1 ] - this reverse the string and we are comparing with user input string.






Output:





GitHub Reference:


Github code:


https://gist.github.com/pinkpretty/989131c070cc4591a3147c803015e5fd


Happy coding !:)

List Overlap With Example Explained - practicepython Exercise - 5

This is the 5th exercise of practicepython

Exercise:

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:
  1. Randomly generate two lists to test this
  2. Write this in one line of Python
I have solved this in 2 ways. Conventional for loop and in one line statement:


For loop Solution


output:




One line Solution:



output:








GitHub Reference:




Github code for loop:


github code for single line:


https://gist.github.com/pinkpretty/ac7ace0e48d519456cd59c1e4a97465a
https://gist.github.com/pinkpretty/b6f1762efcb37a4f69e6f576505e7e39


Happy coding ! :)



Monday 19 September 2016

Divisors - PracticePython Exercise - 4

Exercise

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.)


There is an easy way to create list. 
 x = range(1,9) which will contain [1,2,3,4,5,6,7,8]


Solution:




Output :



Git Hub reference:


Github code


https://gist.github.com/pinkpretty/f3d4773c334ae24a1dc31f6f8a543ba2


Happy coding! :)

List comprehension explanation with Example - Python

List Comprehension is a concept where you can use to construct list in easy way and it is implemented in python 2.0
It provides a compact way of mapping a list into another list by applying a function to each of the elements of the list.


Lets look at the Example with For loop :






And the output is





This can be done using List comprehension in single line. List Comprehension works on expression.


Exercise:


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:
  1. 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.
  2. Write this in one line of Python.
  3. Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.
My Solution:





GitHub Reference: GitHub code


https://gist.github.com/pinkpretty/380b97022f100e37960d2ed0438a6fc5


Happy Coding ! :)



Find Odd or Even in Python: 2rd Exercise of PracticePython



This is the2nd exercise practice Python for beginner


An easy way to find the number is odd or even


Exercise:


Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?
Extras:
  1. If the number is a multiple of 4, print out a different message.
  2. Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.
Solution:



GitHub reference:


Github code


https://gist.github.com/pinkpretty/ffd1cbceb82e1ebaf87761e07ef6644a


Happy coding ! :)