Hackerrank Staircase solution not accepted [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
A hackerrank problem demands you to print a staircase out of hashes:
#
##
###
####
#####
######
I've submitted the following code:
n = int(input())
for i in reversed(range(n)):
print(i*' ','#'*(n-i))
It wasn't accepted. Why?

The problem is your print statement.
print(i*' ','#'*(n-i))
If you print multiple strings separated by a comma, you will get the strings delimited by a space character. E.g.
>>> print("foo", "bar")
foo bar
>>> print("foo"+"bar")
foobar
You can combine two strings with the + operator.
Making this small change in your program should solve the problem.

The print() function prints its arguments separated by a space character (" ") by default, giving you extra characters in the output. You need to either print a single argument, or pass sep="":
print(i*' ' + '#'*(n-i))
or
print(i*' ', '#'*(n-i), sep="")

Related

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)

How to program to only input words and not integers on 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 5 years ago.
Improve this question
In my program, I wish to make the input to be words and not integers, and the inputted words are put into an array. The following variables are already declared.When I run my test data, numbers are accepted into the array.
i=o
for i in range (carers):
nm=input("Enter name")
carernm.append[nm]
You could use isdigit to check if your string contains digits
Example 1
print( all( not c.isdigit() for c in "String doesn't contain any numbers." ) )
True
Example 2
print( all( not c.isdigit() for c in "String 2 doesn't contain any numbers." ) )
False

shifting numbers in python code [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
Please help I have no idea on how to write this function. I tried a ceaser cypher function and it didn't work. Any ideas?
Write a function cycle( S, n ) that takes in a string S of '0's and '1's and an integer n and returns the a string in which S has shifted its last character to the initial position n times. For example, cycle('1110110000', 2) would return '0011101100'.
The function you are looking for is:
def cycle(s, n):
return s[-n:] + s[:-n]
You could use Python's deque data type as follows:
import collections
def cycle(s, n):
d = collections.deque(s)
d.rotate(n)
return "".join(d)
print cycle('1110110000', 2)
This would display:
0011101100

Get specific words from a string in 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 7 years ago.
Improve this question
I want to to extract some information from a data file. The following is the format I have in my data file:
44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)
From this line, I want to extract 256 which is the last number and 4 which is the first number from
(4,0x00002aab 4c000c00)
Could you please recommend some functions which will be useful for my case?
You should use str.split().
What it does is split the string every place there is a space, so you would get a list of strings like so:
n = '44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)'
o = n.split()
print o
Output:
['44', '2.463181s>', '(G)', 'GET_NBI:', '0x00002aaa', 'ecc00e90', '<-', '(4,0x00002aab', '4c000c00)', '(256', 'bytes)']
Then simply get the second-to-last index like o[-2] -> '(256'
Remove the extra parenthesis: '(256'[1:] -> '256', and If you wanna, turn it into an integer. int('256') -> 256
You could also use regular expressions, which in this case might be a bit more clear.
import re
txt = "44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)"
results = re.findall(r"\((\d+)", txt)
# ["4", "256"]

How to print a returned tuple in another method [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 9 years ago.
Improve this question
If I have two methods:
one is a method that changes a string into a tuple and returns it.
other method prints out the tuple returned.
At the end how would I do this?
Input would be something like "12345 firstName lastName otherInfo".
def information(sentence):
splitUp = sentence.split(' ')
return sentence
def printAsString():
"".join(sentence)
print(sentence)
Here is one way to do it. But what is your objective? This code is kind of pointless...
def information(sentence):
'''Splits the sentence on space and returns it as tuple'''
split_up = tuple(sentence.split(' '))
return split_up
def print_as_string():
'''Prints tuple as string'''
sentence = "welcome to SO dear friend"
print(" ".join(information(sentence)))
if __name__ == "__main__":
print_as_string()
It would be
print ''.join(employeeInfo)
But my question is where on earth did employeeInfo came form?

Categories