From bbe38d2e5598241057719e15da1925bc28b5f93f Mon Sep 17 00:00:00 2001 From: nehashirude <44503429+nehashirude@users.noreply.github.com> Date: Wed, 31 Oct 2018 23:11:13 +0530 Subject: [PATCH 1/4] The program takes a number and reverses it. Problem Solution 1. Take the value of the integer and store in a variable. 2. Using a while loop, get each digit of the number and store the reversed number in another variable. 3. Print the reverse of the number. 4. Exit. --- reverse.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 reverse.py diff --git a/reverse.py b/reverse.py new file mode 100644 index 0000000..1f84f91 --- /dev/null +++ b/reverse.py @@ -0,0 +1,7 @@ +n=int(input("Enter number: ")) +rev=0 +while(n>0): + dig=n%10 + rev=rev*10+dig + n=n//10 +print("Reverse of the number:",rev) From 788fb5799780ca4dcdfc98eca1656dc796d991c9 Mon Sep 17 00:00:00 2001 From: nehashirude <44503429+nehashirude@users.noreply.github.com> Date: Wed, 31 Oct 2018 23:34:57 +0530 Subject: [PATCH 2/4] Infix and Postfix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit « How to make IRC bot in python? – my first post in TuxopiaLinked list implementation in C » Infix To Postfix conversion in python September 22, 2009 by Rag Sagar.V രാഗ് സാഗര്‍.വി About Infix and Postfix In an expression if the operators are placed between the operands, it is known as infix notation ( eg A+B) . On the other hand if the operators are placed after the operands then the expression is in postfix notation .( eg AB+) Infix Notation Postfix Notation (A-C)*B AC-B* A+(B*C) ABC*+ (A+B)/(C-D) AB+CD-/ --- Infix_and_Postfix.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Infix_and_Postfix.py diff --git a/Infix_and_Postfix.py b/Infix_and_Postfix.py new file mode 100644 index 0000000..cde2b3a --- /dev/null +++ b/Infix_and_Postfix.py @@ -0,0 +1,58 @@ +#!/usr/bin/python +#https://ragsagar.wordpress.com + +postfix = [] +temp = [] +operator = -10 +operand = -20 +leftparentheses = -30 +rightparentheses = -40 +empty = -50 + +def precedence(s): + if s is '(': + return 0 + elif s is '+' or '-': + return 1 + elif s is '*' or '/' or '%': + return 2 + else: + return 99 + +def typeof(s): + if s is '(': + return leftparentheses + elif s is ')': + return rightparentheses + elif s is '+' or s is '-' or s is '*' or s is '%' or s is '/': + return operator + elif s is ' ': + return empty + else : + return operand + + +infix = raw_input("Enter the infix notation : ") +for i in infix : + type = typeof(i) + if type is leftparentheses : + temp.append(i) + elif type is rightparentheses : + next = temp.pop() + while next is not '(': + postfix.append(next) + next = temp.pop() + elif type is operand: + postfix.append(i) + elif type is operator: + p = precedence(i) + while len(temp) is not 0 and p <= precedence(temp[-1]) : + postfix.append(temp.pop()) + temp.append(i) + elif type is empty: + continue + +while len(temp) > 0 : + postfix.append(temp.pop()) + +print "It's postfix notation is ",''.join(postfix) From 36ffd49294f69601613a5bc13817d1786700086a Mon Sep 17 00:00:00 2001 From: nehashirude <44503429+nehashirude@users.noreply.github.com> Date: Wed, 31 Oct 2018 23:39:15 +0530 Subject: [PATCH 3/4] Python Program to Check Prime Number Example to check whether an integer is a prime number or not using for loop and if...else statement. If the number is not prime, it's explained in output why it is not a prime number. --- prime.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 prime.py diff --git a/prime.py b/prime.py new file mode 100644 index 0000000..e387c31 --- /dev/null +++ b/prime.py @@ -0,0 +1,22 @@ +# Python program to check if the input number is prime or not + +num = 407 + +# take input from the user +# num = int(input("Enter a number: ")) + +# prime numbers are greater than 1 +if num > 1: + # check for factors + for i in range(2,num): + if (num % i) == 0: + print(num,"is not a prime number") + print(i,"times",num//i,"is",num) + break + else: + print(num,"is a prime number") + +# if input number is less than +# or equal to 1, it is not prime +else: + print(num,"is not a prime number") From dbacc8eadd6b08abde965580ef8f44ce6047a35f Mon Sep 17 00:00:00 2001 From: nehashirude <44503429+nehashirude@users.noreply.github.com> Date: Wed, 31 Oct 2018 23:40:27 +0530 Subject: [PATCH 4/4] Rename prime.py to 1prime.py Example to check whether an integer is a prime number or not using for loop and if...else statement. If the number is not prime, it's explained in output why it is not a prime number. --- 1prime.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 1prime.py diff --git a/1prime.py b/1prime.py new file mode 100644 index 0000000..e387c31 --- /dev/null +++ b/1prime.py @@ -0,0 +1,22 @@ +# Python program to check if the input number is prime or not + +num = 407 + +# take input from the user +# num = int(input("Enter a number: ")) + +# prime numbers are greater than 1 +if num > 1: + # check for factors + for i in range(2,num): + if (num % i) == 0: + print(num,"is not a prime number") + print(i,"times",num//i,"is",num) + break + else: + print(num,"is a prime number") + +# if input number is less than +# or equal to 1, it is not prime +else: + print(num,"is not a prime number")