We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Post 83
Python 02 - More Hacker Rank
Published on: 2025-11-05
Tags:
Blog, Hacker Rank, Python
I wanted to add in some of the things that I learned while doing some more hacker rank with Python.
As with many things in Python you can iterate over anything that is iterate-able. I was trying to find the average score of a dict that had a list of scores {name: list_of_scores, ...}
I was able to simply do :
for score in student_marks[query_name]:
total += score
You are taking the entry with the name you want and then iterating over the scores.
Next I wanted to find the runner up score from a list. I was able to sort the list and then pull the first value list[0] and then test for if the value is not equal to the top then break
for score in sorted_list:
if score != top:
print(score)
break
Next I needed to print the list of points on a 3d plane that fall in a range and don't sum to a specific value. I used a nested for loop to take care of this.
string = "["
first = True
for i in range(x + 1):
for j in range(y + 1):
for k in range(z + 1):
if i + j + k != n:
if not first:
string += ", "
string += f"[{i}, {j}, {k}]"
first = False
string += "]"
Next I needed to print the second lowest scores from a list of names and scores. We already found out how to sort a list based off to factors before but Ill put the code here as well
sorted_by_score = sorted(name_score, key=lambda x: (x[1], x[0]))
Once we have that list sorted I was able to find the second lowest I used a for loop to do this as there might me ties for the lowest score. Then I ran an other loop to only print the names for the scores that are the lowest.
name, lowest = sorted_by_score[0]
# print(name, lowest)
for name, score in sorted_by_score:
# print(name, score)
if score != lowest:
second = score
break
# print(second)
for name, score in sorted_by_score:
# print(name, score)
if score == second:
print(name)
if score != second and score != lowest:
break
Lastly I wanted to parse the information from an XML sheet. There was already some code to get the tree for all the XML data. With that I was able to start to parse the data with this.
def get_attr_number(node):
total = len(node.attrib)
for tag in node:
# print(tag.attrib)
total += get_attr_number(tag)
return total