Skip to main content

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)

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]:
                           count = count + 1
                    if count == length/2:
                        palin.append(int(num))


else:
                    length = length - 1
                    for inner in range(0, int(length/2)):
                        if num[inner] == num[len(num) - 1 - inner]:
                           count = count + 1
                    if count == int(length/2):
                        palin.append(int(num))

palin[len(palin)-1]

You should get the desired output from this program.

Keep reading Python an hour a day to learn simple Python tricks, tips and program.

Comments

Popular posts from this blog

Project Euler Problem 13

This problem seems difficult at first, but once you understand it; this will prove out to be easiest of all the problems in Project Euler: I guess you will have seen the problem till now, if not then visit Project Euler 13 else scroll down for this problem. The Solution: The number mentions in the problem is of 100 line and in one line there are 50 numbers. So basically you just have to add all the 100 numbers (each number consist of 50 digits).  you will need str.split() function to do the job. num = "37107287533902102798797998220837590246510135740250 \n \ 46376937677490009712648124896970078050417018260538 \n \ 74324986199524741059474233309513058123726617309629 \n \ 91942213363574161572522430563301811072406154908250 \n \ 23067588207539346171171980310421047513778063246676 \n \ 89261670696623633820136378418383684178734361726757 \n \ 28112879812849979408065481931592621691275889832738 \n \ 44274228917432520321923589422876796487670272189318 \n \ 474514457360013064...

Project Euler 14:Longest Collatz sequence

The Problem: The following iterative sequence is defined for the set of positive integers: n → n /2 ( n is even) n → 3 n + 1 ( n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1   It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? NOTE: Once the chain starts the terms are allowed to go above one million. The Solution: We can divide the solution in 3 task. To form the chain of sequence. To feed the data to the first function. Third will be sort the values.  To form the chain of sequence: def chain(n):        a = []  # will hold the sequence     a.append(n)     while n...

Project Euler 11: What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

You guys can get this programming ask from Project Euler. This  is the 11th question  of this series. The solution of this question needs you to be good in matrix. You can find the solution below. The Solution: You have to find the greatest product of the four adjacent number and the number can should be in the same direction. So basically, if we start by first number of the series 08; then we can find only 3 adjacent number in the same direction and that will be: 1>  08 02 22 97(going in positive direction) 2>  08 49 81 52 (Going in down direction) 3> 08 49 31 23 (Going in positive diagonal direction) but a number can have maximum 8 products in the same direction; for better understanding we have marked it in the red and you can also check the below image for better understanding. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67...