How to convert the output into a single line [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
#python
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
rev=arr[::-1]
for i in range(n):
final=0
final+=rev[i]
print(final)
Given an array of integers, print the elements in reverse order as a single line of space-separated numbers.

You are calling input() many times which is not required, also you can use the join function with " " separator to print all the elements of array in one line. Please note that join expects all elements as str so you will have to map it back to str, so it's better to avoid the map to int at the initial stage as we will have to map it back to str
Here is one of the approach:
n = input("Enter the sequence of numbers: ").strip()
arr = n.split()
print (" ".join(arr[::-1]))
Output:
Enter the sequence of numbers: 1 3 5 7 9
9 7 5 3 1

u may use join method for concat string sequences
but in your case u need to transform integers to string type
in the end code will be like this:
reversed_string = ' '.join(map(str, rev))

Related

change the order of numbers from the back [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
they need to order with a problem
so I have the text in a certain order of numbers, something like gematria
input [12345] is what we call gematria and what do they need?
they need to line up the digits backwards
[54321]
have a different count and I would need help with that rather than doing twenty different if
def shiftall(s, n):
n %= len(s)
return s[n:] + s[:n]
it didn't help me much it only moves the simple text
For strings:
return s[::-1]
For integers:
return str(s)[::-1]
Note: This would go inside def shiftall(s, n):
Additional note: Now you don't even need the parameter n
If you want to reverse a number, then you can convert it to a string, reverse the string, and then convert it back to a number.
num = 12345
str_num = str(num)
# reverse and convert
num = int(str_num[::-1])
input=[12345,43545436,88888,843546]
def shiftall(s):
d=[]
for i in s:
res=''.join(reversed(str(i)))
d.append(int(res))
return d
print(shiftall(input))

Python - How to keep smallest number (converting from string to int) in list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If I have a list of elements such as:
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
I want to keep the elements with the smallest number after the second " - ". However, they must be compared with the elements which have the same first two numbers in the string.
For e.g. the strings which start with 058529-08704, the smallest number is 058529-08704-140200 and for 058529-08705, the smallest number is 058529-08705-140200
So the final list must end up with ["058529-08704-140200", "058529-08705-140200"].
What is the most pythonic way to achieve this instead of having to write multiple ifs or using string manipulation?
items = ["058529-08704-200280", "058529-08704-230330", "058529-08704-140200", "058529-08704-290390",
"058529-08705-140200", "058529-08705-230330", "058529-08704-170240", "058529-08705-290390",
"058529-08705-170240"]
lst_3th_num = []
for item in items:
lst_3th_num.append(int(item.split('-')[2]))
result = []
for item in items:
if int(item.split('-')[2]) == min(int(s) for s in lst_3th_num):
result.append(item)
print(result)

i cant sum the numbers (python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Write a shell (text-based) program, called sum_num.py, that asks the user for a semicolon (;) separated list of numbers, and calculated the total. See the example below.
a = str(raw_input("Enter semicolon separated list of integers:"))
b = a.split(";")
c = (a[0:])
print("the total is " + sum(c))
PS C:\Users\ssiva\Desktop> python sum_num.py
Enter semicolon separated list of integers: 3;10;4;23;211;3
The total is 254
This code will convert into integers and sum them
a=input()
b=a.split(';')
sum=0
for num in b:
sum+=int(num)
print(sum)

a line of numbers (python) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm not a programmer, I'm just starting to learn python 2.
I wanted to know how can I write a code in python 2 to get an input that consists a line of numbers seperated by space from the user and do operations with each number sepeately and print the output of each number, again, as a line of numbers seperated by space?
The below example will loop complete the action you were looking for, just replace the "+5" with the operation you want
userInput = raw_input("Enter Numbers:")#Get user input
seperateNumbers = userInput.split(" ")#Seperate data based off of spaces
numbersAfterOperations = []#Create a lsi to hold the values after operation applied
for i in range(0, len(seperateNumbers)): #loop throgh all values
numbersAfterOperations.append(int(seperateNumbers[i]) + 5) #add new value
printedValue = ""
for i in range(0, len(numbersAfterOperations)):
printedValue += str(numbersAfterOperations[i]) + " "
print printedValue
You will start with a string containing all your numbers, so you will have something like this:
line_of_num = "0 1 2 3 4 5 6 7 8 9"
You will have to split the elements of this string. You can do it using the split method of the string class, using a space as separator. It will return a list of the items with your numbers, but you can't operate them as integers yet.
list_of_num = line_of_num.split(" ")
You can't operate them yet because the elements of your list are strings. Before operating them, you have to cast them into integers. You can do it using list comprehentions.
list_of_int = [int(element) for element in list_of_num]
Then you can operate them using common operations through elements of a list. Finally, when you have the results, you can return them as a string separated by spaces using the join method of the string class using a space as separator. The join method has as input an iterable (list, for example) of strings.
results = " ".join(["1", "2", "3", "4", "5"])
Your resulted string will be something like "1 2 3 4 5".

How would I create a Python program that reads a list of numbers, and counts how many of them are positive [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create a Python program that will count how many positive numbers there are in a list of numbers. The list of numbers has to be typed in by someone. The end result must be the number of elements in the list that were > 0
For an example, this is what you would see on the screen:
>>>Please enter a list of numbers separated by commas: 1,2,-3,-4,5,-6
>>>3
The answer would be 3 in this example. I am sorry if the question seems stupid, but I am a beginner and I am trying my best.
raw_input() for Python 2.x (input() for Python 3) then split() the string at , and then count positive numebers, Example -
s = raw_input("Please enter a list of numbers separated by commas:")
print(len([i for i in s.strip().split(',') if int(i) >= 0]))
You can try like this. input returns tuple
>>> vals = input('get: ')
get: 1,2,-3,-4,5,-6
>>> len([item for item in vals if item > 0])
3
Python 3, input returns string
>>> vals = input('get: ')
get: 1,2,-3,-4,5,-6
>>> len([item for item in vals.split(',') if int(item) > 0])
3
By the way, zero is neither positive nor negative.

Categories