While solving some progrmaming problems, I've noticed that the dialogue says:
Input:
Integer N denoting size of array
Next line contains N space separated integers denoting elements in
array
How am I supposed to use the variable N so that it is functioning as supposed to and not just a useless-floating-around input-variable...
I mean, it could just be one input denoting the elements of the array, no need for the length of its elements...
PS: I know that I can just add it there just to pass the problem, I am just asking about if that N variable could be useful using Python (Without the way of a for loop, to ask N number of times for input cause it won't pass the problem).
In Python it usually is, since usually you fetch the entire line at once and process it.
Some programming languages like C++ and Java however tend to benefit from this approach since some parser tools (like Java's Scanner) work by parsing one integer at a time.
You can simply parse your input like:
input() # ignore the 'N'
arr = [int(x) for x in input().split()]
x=list(map(int,input().split())
print(x)
Try this
you will get an array of integers
If I understood you correctly, you want space-separated numbers with the length defined as the input. You can achieve that by:
N = input("Integer N denoting size of array: ")
print(" ".join(str(i + 1) for i in range(int(N))))
e.g.:
Integer N denoting size of array: 12
1 2 3 4 5 6 7 8 9 10 11 12
Related
Actually, I have written a basic code, which includes input command for that:
f23=float(input("Enter The 23 Element of Array:"))
but it does not have any limitation with digits, and I'd like to restrict it up to 3 digits. for example : 2.253
does anybody know what to do?
if you want to auto-correct the user then you should use the builtin round function or the format function.
or, if you want to raise an exception when it does that, i would suggest to equate the rounded and the original version.
a basic implementation would be something like this.
arr = []
no_digits = 3
for x in range(26):
arr[x] = round(float(input(f"the {x}th element is: ")),no_digits)
or with list comprehension:
arr = [round(float(input(f"the {x}th element is: ")),no_digits) for x in range(26)]
#card number
card = input('Number: ')
j = int(card[::2]) # this will jump character by 1
# multiplying each other number by 2
j *= 2
print(j)
So whenever I run this code and input e.g. 1230404
The output would be correct which is 2688
But when I input for example 1230909 the output is 2798, I expected 261818
Let's look at what your code is doing.
You slice every second character from your input string, so '1230909' becomes '1399'.
You convert that to a single int, 1399.
You multiply that number by 2, producing 2798. I assure you that the computer did the math correctly.
It appears what you expected was for each digit to be doubled individually. To do that, you need to convert each digit, double it, and combine them back. Python has great facilities for this, I'd suggest a generator expression inside a join call.
I am trying to convert an int to a binary string of length n using format function. I know how to do it for a fixed value of n, say n=3 and I want to convert integer 6 to a bitstring of length n=3, then I can do format(6, '03b').
May I know how to do the same for any given n? Clearly I cannot do format(6,'0nb'). So I'm wondering how to solve this?
You can use a formatted string to dynamically insert the number n into the string:
n = 6
format(6, f'0{n}b')
This answer is maybe a little misspoint but it use exact number of bit that are necessary to represent value, not a fixed value like format(x, f"{n}b"). Where x is some number, and n is number of bits.
import math
n = 6 # your number
format(n, f'0{int(math.log2(n))}b')
Here n is the number like x before, and there is no n, because we dynamicaly calculate right number of bits to repr n
I have some kind of AP in Computer Science and I've started on coding/programming with Python. I briefly used C++ for few years and now I switched to Python. I don't really remember where I stopped with C++ but that's irrelevant now. Anyways, I have this task that says: "Write program that loads number N, then N numbers and prints number that has the highest value between them. Number N has input in one line and line underneath loads N numbers with same space between them. None of the numbers will be greater than 100. Number N will be greater than 2."
I wrote this code;
`n = int (input())
max = 2
for i in range (1, n+1, 1):
x=map(int,input().split())
if x>max: x=max
print (max)
`
which returned this error:
5
2 87 12 90 55
File "C:\Users\Mariee.Marija-PC\Documents\Python\19-4.py", line 5, in
if x>max: x=max
TypeError: unorderable types: map() > int()
that was totally expected because I know I can't compare those two as they are clearly not comparable (which again, I am very aware of).
So my question is, is there any other way that N number could be put in one line and then N numbers could be put in the same line but you can compare them (if that makes any sense).
[P.S. Also I'm ultimately sorry if my english was bad.]
The variable x is a map (similar to a list) of integers, but you are comparing it with a single integer.
In order to loop over the values of x you should use a for loop:
for x in map(int, input().split()):
if x > max:
max = x
There's already a max method in Python, and no need to re-create a max function like you have to do in C/C++. Just apply the max method
n = int (input())
print (max(map(int,input().split())))
I am trying to make a code that does the following:
Multiplying the digits of an integer and continuing the process gives
the surprising result that the sequence of products always arrives at
a single-digit number.
For example:
715 -> 35 -> 15 -> 5
88 -> 64 -> 24 -> 8
27 -> 14 -> 4
The number of products necessary to reach the single-digit
number is called the persistence number of that integer. Thus 715
and 88 have a persistence number of 3, while 27 has persistence 2.
Make a program to find the only two-digit number with persistence
greater than 3?
I was able to come up with a rough idea and the code is below but it doesn't seem to work:
num2=0
num3=0
num4=0
num=input("what is your number?")
while num in range(10,100):
print 'step1'
num1=num%10*num/10
if num1-10>10:
print 'step2'
num2=num1%10*num1/10
elif num2-num1>10:
print 'step3'
num3=num2%10*num2/10
elif num3-num2>10:
print 'step4'
num4=num3%10*num3/10
elif num4-num3>10:
print 'step5'
print num4
else:
break
The program is Python and I simply can't figure this out. If someone could possibly help me I would appreciate it greatly!
You should use a while or for loop to multiply the digits instead of hardcoding what to do with the first, second and so on digits.
In pseudocode...
productSoFar = 1
digitsLeftToMultipy = #the number
while there are digits left to multiply:
get the next digit and
update produtsSoFar and digitsLeftToMultiply
Also, use
10 <= n < 100
instead of
n in range(10, 100)
So you only do a couple of comparisons instead of a sequential lookup that takes time proportional to the length of the range.
Functions are friends.
Consider a function, getEnds(x), which when passed an integer, x will extract the first digit and the last digit (as integers) and return the result as a tuple in the form (first_digit, last_digit). If x is a single-digit number the tuple will contain one element and be in the form (x), otherwise it will be two. (A simple way to do this is to turn the number into a string, extract the first/last digit as a string, and then convert said strings back into numbers... however, there are many ways: just make sure to honor the function contract, as stated above and -- hopefully -- in the function documentation.)
Then, where n is the current number we are finding the persistence for:
ends = getEnds(n)
while ends contains two elements
n = first element of ends times second element of ends
ends = getEnds(n)
# while terminates when ends contained only one element
# now it's only a matter of "counting" the persistence
For added points, make sure this is in a -- [an] appropriately named/documented -- function as well and consider the use of a recursive function instead of a while-loop.
Happy coding.
If you're trying to get the digits of a number, convert it into a string first and reference them with array notation.