I'm trying to make a triangle of number like this one here
1
12
123
12
1
Number of line is chosen by the user. For example if user enters : 5. There must be 5 line (like in the previous triangle). And the number must be an odd number.
Here is what I have done so far but i'm stuck.
oddNumber = int(input("Enter an odd number here:"))
for i in range(oddNumber):
for j in range(oddNumber):
print("", end="")
for k in range(1,i):
print(k, end="")
print("")
The output of that is (if I enter 5):
1
12
123
Related
I feel stupid for already having to ask questions this early into learning python but the task is this:
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
I have not started on the try/except part yet but with my current code it seems to only be running the full loop once then getting stuck on the largest number check.
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" : break
if largest is None or num > largest :
largest = num
print("large", largest)
if smallest is None or num < smallest :
smallest = num
print("small", smallest)
print("Minimum is", smallest)
print("Maximum is", largest)
The output I get just takes the first number as min and last number as min.
Enter a number: 10
Enter a number: 17
Enter a number: 20
Enter a number: 3
Enter a number: 6
Enter a number: done
Minimum is 10
Maximum is 6
I'm stumped any help would be much appreciated.
You're comparing strings, so it's doing a lexicographical order, where 6 is larger than 10. You need to convert the number strings to actual numbers before comparing:
str_num = input("Enter a number: ")
if str_num == "done":
break
num = int(str_num) # Convert to integer
Input format: The first line of input consists of the number of test cases, T
Next T lines consist of the value of N.
Constraints: 1<= T <=100, 1<= N <= 250
Output format: For each test case, print the space-separated N terms of the series in a separate line.
Sample test case 1
Input:
1
7
Output:
1 1 2 2 4 2 6
The series is a combination of 2 series, the 1st series: 1,2,4,6,... and the 2nd series: 1,2,2,.... I have made the code for the first series but cannot find how to code the 2nd one.
Code for the first series appended into list depending on the no of elements
def firstS:
l=[1]
i=1
x=math.ceil(7/2)
while(x!=0):
l.append(i+i)
i+=1
x-=1
return l
The problem is the no of elements, for 7 elements the 1st series has 4 and 2nd series has 3 elements, for 8 elements 1st has 4 and 2nd has 4 elements and for 9 elements 1st has 5 and 2nd has 4 elements so the no of elements will be for series 1 math.ceil(n/2) and for series 2 math.floor(n/2) where n is total elements of the combined series.
For iteration, one way do something every N iterations is to use the modulus operator (%). Modulus is basically a remainder operator, so the result periodically repeats as numbers are iterated one-by-one.
Also, in Python, the standard method for doing a for-loop (iterating a certain number of times) is using range.
Here's an example demonstrating both, where every third number has the same number of exclamation marks:
# List the numbers 0-9 (repeat ten times)
for i in range(0, 10):
if i % 3 == 0:
print(i, "!")
elif i % 3 == 1:
print(i, "!!")
else:
print(i, "!!!")
Result:
0 !
1 !!
2 !!!
3 !
4 !!
5 !!!
6 !
7 !!
8 !!!
9 !
I'll leave it as an exercise for the asker to determine how to apply this to their use-case of switching between printing two different sequences.
Super-sum S of an integer x is defined as x if x is single digit number otherwise Super-sum of x is defined as Super-sum of digit-sum of x. Given two numbers n,k find the Super-sum of the number formed when n is concatenated k times.Note that k is only multiplied with the number when it is at least a 2 digit number
Input:
1987 4
Output:
1
Is there a faster method than this?
s,k=input().split()
summ=0
for ele in s:
summ+=int(ele)
s=summ*int(k)
while s>=10:
s=str(s)
summ=0
for ele in s:
summ+=int(ele)
s=summ
print(s)
n,k=map(int,input().split())
if n<10:
print(n)
else:
if ((n*k)%9==0):
print(9)
else:
res=(n*k)%9
Any number greater than 9 will have digits repeated that's why you need to take mod of 9 for example 13 will have sum of 1+3 =4 and 13 %9=4 .There will be a special case when mod of 9 will be zero and this will be at number 9,18,27,36 etc which are divisible by 9 and their sum will always be 9 hence return 9 .
The formula used in the accepted answer should actually be proved. So, let's do this.
First, let's prove that for any positive integer number N, N modulo 9 is the same sum of its digits modulo 9.
Having proved the statement above, we easily conclude that N % 9 provides the needed digit as per challenge description. The only exception is if N % 9 == 0, then a digit is 9.
this my 5th day of learning programming in python. Now I looking solution to print a line which will be long as inputted by user text. For simple program is easy. The three hours of manipulating python commands gave me that what I wanted.
print('Write any word:')
inputted_word = input()
print("-" * int(len(inputted_word)))
print("Your word has got: ",len(inputted_word), "characters.")
Screen:
Write any word:
python
------
Your word has got: 6 characters.
Yes, for me is better to find a solution by myself rather than get it from the web.
However, I would like to make a table (in this case multiplication tables). A user should be input a multiplication number. Ok, this is done.
print('Write any number:')
d = input()
for i in range(1, int(d)+1):
print(i, end=' ')
for j in range(1,11):
print(j, end=' ')
print()
Screen (don't worries about the same numbers):
Write any number:
4
1 1 2 3 4 5 6 7 8 9 10
2 1 2 3 4 5 6 7 8 9 10
3 1 2 3 4 5 6 7 8 9 10
4 1 2 3 4 5 6 7 8 9 10
But how to count the number of characters in 1st line to achieve exactly number to create a line? I presume we need to go out from the loop. So, how to make this? As well as how to make vertical line divided the first column? Any tips, solution for the beginner user, who spent the whole day to reading python documentation and info from the web?
Let's break things up into functions. We want the main part of our program to look like this:
word = get_word() # e.g. "python"
print_table(len(word)) # makes table of 6 characters
So let's write our functions. The first one is exactly what you wrote:
def get_word():
print('Write any word:')
word = input()
print("-" * int(len(word)))
print("Your word has got: ", len(word), " characters.")
return word
For the second function, we can do it similar to your way:
def print_table(n):
for i in range(1, n + 1):
print(i, end=' ')
print('|', end=' ') # vertical column
for j in range(1, 11):
print(j, end=' ')
print()
The way I'd do it personally is using something called a "list comprehension" and the str.join() function to make each row first:
def print_table(n):
# Make a string containing '1 2 3 4 5 6 7 8 9 10'
row = ' '.join(str(x) for x in range(1, 11))
# Print n rows
for i in range(1, n + 1):
print(str(i) + ' | ' + row)
if we are given a mobile phone with number pad as
9 8 7
6 5 4
3 2 1
* 0 #
and a number n , then how many numbers of n digit we can make my typing in keypad , we can not move diagonally from a previously chosen number i.e from if we have typed 9 the next no i can choose is 8 or 6 . Also number like 082 will be count as 2 digit number not 3.
sample test case
input n = 1 output = 9
input n = 2 output = 25
I am unable to formulate a dynamic programming/backtracking solution for it .
Let f(n) be the list of all sequences composed of n digits.:
The base case is simple: f(1) = [ 1, 2, ..., 9 ].
In the general case, how can you compute f(n) from f(n-1)? Simply loop over the elements of f(n-1) and check the last digit. Say f(2) contains 85, then f(3) should contain 852, 854 856, and 858. Add all these new elements in a new list and return it.