Skip to main content

Posts

Showing posts from June, 2018

Get the list of all the files and directories

Function Use: To print all the directories in a folder/sub-folder or directory import os def get_list_or_dir(local_dir):     for dir_or_file in os.listdir(local_dir):         if os.path.isdir(dir_or_file):             path = os.path.join(local_dir, dir_or_file)             print(dir_or_file, 'This is a directory')             get_list_or_dir(path)         else:             print(dir_or_file) Path = os.getcwd() or define the path from where you need a file Function Call: get_list_or_dir(path)

Project Euler: Special Pythagorean Triplet

You guys can get this programming question from Project Euler.   The code for this ask is :   for a in range(1,1000):     for b in range(1,1000):         c = math.sqrt(a*a + b*b)         if a + b + c == 1000.0:             print a*b*c You guys will get the answer once you execute the code.

Project Euler: What is the 10001st prime number?

We have already wrote a similar program on Prime Number:  So, before explaining anything about prime number we will directly jump in program.  Programme for What is the 10001st prime number? n = 2 a = [] while n < n+1: sum = 0 for i in range(1, n+1): if n%i == 0: sum += 1 if sum == 2: a.append(n) if len(a) == 10001: break n = n + 1 This shall give us all the prime number till 10001st. Type a[len(a) - 1] into the console to get the output. Keep reading Python an hour a day to learn simple Python tricks, tips and program.

Find the largest palindrome made from the product of two 3-digit numbers.

Before starting to discuss how to program the above code. We should about palindrome first. Palindrome is a number, word, sequence, list , arry and other which reads same from backward and forward. Palindrome Number: 121, 1111, 999, 58085 Palindrome Word: Madam, My Gym, Radar, wow. Palindrome List: [1,2,3,2,1] In this blogpost we are going to discuss the largest palindrome made from the product of two 3-digit numbers. a = 100 # smallest 3 digit number b = 999 #largest 3 digit number palin = []  #list to keep palindrome number for i in range(a, b+1): for j in range(a, b+1): num = str(i*j) length = len(num) count = 0 if length%2 == 0:                     for inner in range(0, int(length/2)):                         if num[inner] == num[len(num) - 1 - inner]:                         ...

What is the largest prime factor of the number 600851475143 ?

I have got this difficult programming ask from Project Euler. Before jumping into details we should know what is the prime number and prime number factors.. Prime Number:   The number which is divisible only by itself and 1. Example: 1, 3, 5, 11, 13 and so on. These are prime number. Is 9 a prime number? No, 9 is divisible by 1, 3, 9. So, it can't be a prime number. A program to print prime number till 100. >>> n =100 >>> i = 2 >>> for i in range(2, n+1):                count = 0                for j in range(2, i+1):                             if i%j == 0:                      count += 1    ...

Printing Star Pattern in Python - Ep 3

Welcome to the third post about printing patterns in Python. The first 3 basic patterns were easy to print that we have taught in the first 2 blogspot . Today we will learn about printing the below pattern.     *         * *       * * *     * * * *   * * * * *  The Program   >>> n = 5 >>> for i in range(0, n): print((n-i-1)*" "+ i*"* " + "* " + (n-i-1)*" ")     *         * *       * * *     * * * *   * * * * *  The above programme is a simple 3 line programme to print this kind of star pattern. Let's discuss the third line of the code to understand this better. The Maths & Logic: In the first of the output there are 4 spaces" " and then, one star "* "  and then again 4 spaces " ". Total of 9 element : 8 spaces and one ...

Printing Pattern in Python - Ep 2

Welcome to the second post about printing the patterns in Python. Today we will learn about printing the below patterns in Python. 1> * * * * * * * * * * 2> * * * * * * * * * * We will recommend you to read the Printing pattern in Python Ep1 first , as it will help you understand better. 1>  Program to print first pattern. >>> n = 4 >>> for i in range(1, n+1): print(i*"* ") *  * *  * * *  * * * *  2>   Program to print second pattern. >>> n = 4 >>> for i in range(0, n): print((n-i)*"* ") * * * *  * * *  * *  *   Keep reading Python an hour a day to learn simple Python tricks, tips and program.

Printing Pattern in Python - Ep 1

How to print the below pattern in Python: * * * * * * * * * * * * * * * * I will be showing 2 ways to print the above pattern, but there are more ways; you can explore it by practising yourself. Before starting this i want to show you some of the trick that you should know. If you want to print same number, word or anything number of times, you can the use the below code. >>> print(4* "Avenger ") Avenger Avenger Avenger Avenger  Here i wanted to print Avenger 4 times than i just wrote 4*"Avenger " in the print statement and i got the desired output. So, we are good to begin printing the above pattern in python. Method 1: >>> n = 4 >>> for i in range(0, n): print(n*"* ") * * * *  * * * *  * * * * * * * *  I guess i don't need to explain it further, I wanted to print star in 4 times so i just wrote print(n* "* ") to print the star 4 times in a row and i ran the loop 4 times by using ...