Python Staircase (Hackerrank) - python

I'm trying to solve a problem in Hacker rank and they gave me a staircase challenge. I don't see the difference between my output and theirs. Could anyone help me out?
Here's the link to my problem: https://www.hackerrank.com/challenges/staircase/problem.
Here's the code I wrote(only the function, rest were already written):
import math
import os
import random
import re
import sys
# Complete the staircase function below.
def staircase(n):
for x in range(n, 0, -1):
print(" "*(x-1),"#"*(n-x+1))
if __name__ == '__main__':
n = int(input())
staircase(n)
The output screen on the website:

Related

Why can't python ast detect an if statement within a function?

I am a newbie to ASTs as I come from a Statistics background. As per my observation, Python is unable to detect an if statement for the following code -
import ast
from pprint import pprint
tree = ast.parse("""
def add(a, b):
return a + b
def subr(a,b):
if 2>3:
print("true")
else:
print("false")
return 0
""")
for node in ast.iter_child_nodes(tree):
print(isinstance(node, ast.If))
However, if there is no function, it can detect the if statement -
import ast
from pprint import pprint
tree = ast.parse("""
if 2>3:
print("true")
else:
print("false")
""")
for node in ast.iter_child_nodes(tree):
print(isinstance(node, ast.If))
Could someone please tell me what is the problem with my former code block?
#Mathias R. Jessen is correct. iter_child_nodes() can only iterate through immediate child nodes. To iterate through the entire code, one should use ast.walk()

Python - How display, and update value (printed)

I have a python script which receives values in realtime. In a green circle that value need to update in real time. In pink cirkle are line from txt file. I have problem becouse actually i use "os.system("cls")" and print all this but that looks bad when value refresh "fastest", i think you know what's happening.
I want "design interface" it will not update but only value who i want to "refresh". Someone have idea?
Example:
https://i.imgur.com/sAEyB7p.png
Perhaps curses. You will need pip install windows-curses. The following snippet is a piece of what I imagine may suit.
import curses
import time
import random
sometext = "User1: xyz\nUser3: xyz\nUser4: xyz\nUser5: xyz\nUser6: xyz\n"
def main(stdscr):
stdscr.clear()
stdscr.addstr(6, 0, sometext)
for _ in range(0, 10):
stdscr.addstr(2, 2, f"Value: {random.random() * 10: .0f}")
stdscr.addstr(3, 2, f"Value2: {random.random() * 100: .0f}")
stdscr.addstr(4, 2, f"Value3: {random.random() * 1000: .0f}")
stdscr.refresh()
time.sleep(0.5)
curses.wrapper(main)

hackerrank problem runs fine in jupyter but fails in hackerrank

I'm not sure what's going on with this problem.
I place the exact same code in a jupyter notebook and everything runs fine. But when I place the code in Hackerrank it does not return any output.
Does anyone spot the error here?
Sample:
6 4
give me one grand today night
give one grand today
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
# Complete the checkMagazine function below.
def checkMagazine(magazine, note):
ds = Counter(magazine)
for m in note:
ds[m] = ds[m] - 1
if ds[m] < 0 or ds[m] is None: return 'No'
return 'Yes'
if __name__ == '__main__':
mn = input().split()
m = int(mn[0])
n = int(mn[1])
magazine = input().rstrip().split()
note = input().rstrip().split()
checkMagazine(magazine, note)
This code returns but doesn't print the output to stdout that the HR code runner is looking for. Try print(checkMagazine(magazine, note)).
In general, HR is a bit fussy about IO. Data will be read through stdin and will be printed to stdout, often in bizarre formats like "Yes" or "Impossible!" for a function that would normally return a boolean.

Index out of range appears sometimes and disappears when i run again python

import numpy
import matplotlib.pyplot as plt
import serial
V=[]
I=[]
P=[]
count=0
arduinoData=serial.Serial('/dev/ttyACM0',9600) # importing serial data
a=input('enter the no of observation required = ') # user choice to get the required no of observations
while count<a:
count=count+1
while(arduinoData.inWaiting()==0):
pass
arduinoString=arduinoData.readline()
data=arduinoString.split(",")
voltage = float(data[0])
current = float(data[1])
power = float(data[2])
V.append(voltage)
I.append(current)
P.append(power)
print V,",",I,",",P
#plt.plot(V)`enter code here`
#plt.show()
#plt.ylim([150])
I get this error off and on sometimes the code works fine and sometimes index is out of range i'm really confused why is it so
I have just started learning python programming
Have you checked the string to make sure it has three comma separated elements? If it doesn't you may need to treat it as malformed and throw it away.

Python algorithm output troubleshoot

I have attached a python 2.7 script to answer question number 2 from the following link: http://labs.spotify.com/puzzles/
My attempt to solve is currently being met with a "wrong answer" reply, yet my code successfully works for the sample inputs on the site. I have tried modifying it to return and even print a list of the top m songs, instead of printing them out individually, but that did not work either. Any help or ideas would be great. Thanks in advance
import sys
def main():
line1 = sys.stdin.readline().split()
total_songs = int(line1[0])-1
num_songs_return = int(line1[1])
data = sys.stdin.read().split()
while(total_songs >= 0):
data[2*total_songs]= float(data[2*total_songs]) * (total_songs+1)
total_songs-=1
answers = [(data[a], data[a+1]) for a in range(0,len(data),2)]
answers.sort(reverse=True)
for n in range(num_songs_return):
print answers[n][1]
main()

Categories

Resources