Python Answers

Questions answered by Experts: 5 288

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search

Number of digits until N

Given an integer 

N, write a program that prints the count of the total number of digits between 1 and N.

Input

The input is a positive integer.

Output

The output should be a single line containing the count of the digits up to the given number.

Explanation

Given 

N = 10

From 1 to 9, each number contains a single digit. From 10 onwards, the numbers contain two digits.

So the output should be 9 + 2 = 

11.

Sample Input 1
10
Sample Output 1
11
Sample Input 2
4
Sample Output 2
4

sample input 3
1000
sample output3
2893




Question 1:

write a program to find the given string in the root directory that contains a text file

1. In a recursive manner (case in sensitive )

2. only in the root directory

use command line arguments.

program_name -ri string root_dir (recursive)

program_name -si string root_dir (root director )

The output which file and line contains the string


Question 1:

Finder program

# Dict, list, queue

# directory --- os.walk()

list1 = [c:, d:, e:]

dict1 = {}

def func(drive)

resp = os.walk(drive)

statem1

st

dict1[file] = path

for drive in list1:

#th1 = Thread(target= func(drive))

#th1.start()

func(drive)

# apply join

finding total time of execution #

dict ---> pickle


Question 1:

How to make object as callable video __call__

from collections import Counter

class MyCounter(Counter):

Add here required code so that we can give data to object like co1("intel")

co1 = MyCounter()

co1("intel")


Given

rows = 4

cols = 3

matrix = 1 2 3

4 5 6

7 8 9

10 11 12


output : 1 2 3 6 5 4 7 8 9 12 11 10


Explination : Every alternative row should be in reverse order.


Maximum

You are given

N number of inputs. Print the maximum of them after each input.Input

The first line of input is an integer

N. The next N lines each contain an integer as input.Explanation

In the example, you are given

6 inputs 1, 0, 3, 2, 9, 8.

1 is maximum in the input 1.

1 is maximum in the input 1, 0.

3 is maximum in the input 1, 0, 3.

3 is maximum in the input 1, 0, 3, 2.

9 is maximum in the input 1, 0, 3, 2, 9.

9 is maximum in the input 1, 0, 3, 2, 9, 8.

 


So, the output should be


1

3

3

9

9




Last half of List

You are given an integer 

N as input. Write a program to read N inputs and print a list containing the elements in the last half of the list.

Input

The first line of input is an integer 

N. The second line contains N space-separated integers.

Explanation
Sample Output 1

In the example, we are given 

6 numbers 1, 2, 3, 4, 5, 6 as input.

The last half of elements of the list are 

4, 5, 6. So, the output should be [4, 5, 6].

Sample Output 2

In the example, we are given 

5 numbers 1, 11, 13, 21, 19 as input. The last half of elements of the list are 21, 19. So, the output should be [21, 19].

Sample Input 1
6
1 2 3 4 5 6
Sample Output 1
[4, 5, 6]
Sample Input 2
5
1 11 13 21 19
Sample Output 2
[21, 19]

List Indexing - 3

Given 

N numbers, and an index, write a program to store the numbers in a list and print the number at given index. For this problem, each input will contain T testcases. Each testcase will give an index Ki as input, which should be considered to print the number.

Input

The first line of input is an integer 

N. The second line of input is an integer T representing the number of testcases. The next N lines contain integers representing the numbers of the list. The next T lines contain integer Ki for each line.

Output

You need to print a number in a new line for each of the 

K testcases.

Explanation

In the given example, we are given 

4 numbers 1, 2, 3, 4 as input For the first testcase, K=0, the number at 0th index is 1. For the second testcase, K=3, the number at 3rd index is 4. So, the output should be

1
4
Sample Input 1
4
2
1
2
3
4
0
3
Sample Output 1
1
4
Sample Input 2
3
1
13
21
19
0
Sample Output 2
13

Shaded Diamond

Given an integer value 

N as input, write a program to print a shaded diamond of 2*N -1 rows using an asterisk(*) character as shown below.

Note: There is a space after each asterisk (*) character.

Input

The first line of input is an integer 

N.

Explanation

In the given example 

N = 5. Therefore, the output should be

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 *     *
  *   *
   * *
    *
Sample Input 1
6
Sample Output 1
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
 *       *
  *     *
   *   *
    * *
     *
Sample Input 2
5
Sample Output 2
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 *     *
  *   *
   * *
    *

Number Diamond

Given an integer 

N as input, write a program to print a number diamond of 2*N -1 rows as shown below.

Note: There is a space after each number.

Input

The first line of input is an integer 

N.

Explanation

In the given example, the number of rows in the diamond is 

5.

So, the output should be

    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
 1 2 3 4 
  1 2 3 
   1 2 
    1
Sample Input 1
5
Sample Output 1
    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
 1 2 3 4 
  1 2 3 
   1 2 
    1
Sample Input 2
4
Sample Output 2
   1 
  1 2 
 1 2 3 
1 2 3 4 
 1 2 3 
  1 2 
   1

LATEST TUTORIALS
APPROVED BY CLIENTS