HackerRank Day 7: Arrays | Python

preview_player
Показать описание
In this series, I will walk you through Hacker Rank’s 30 days of code challenge day by day.

In Day 7, we will learn about Lists and Arrays.

Try solving it yourself!

View my solution for Day 7 at

Join our LinkedIn Group to ask questions and learn from others.

Support me on Patreon!

#OTSC #HackerRank #Python
Рекомендации по теме
Комментарии
Автор

Alternate Solution:
Convert array in reverse order, store it in tuple and iterate over it.
arr2=tuple(arr[::-1])


for i in arr2:
print(i, end =" ")

arjuns
Автор

This was my approach

if __name__ == '__main__':
    n = int(input())

    arr = list(map(int,  input().rstrip().split()))
arr.reverse()

for i in arr:
    print(i,  end=" ")

Uchiha_Mako
Автор

Simpler Alternate Solution:
We get reversed tuple and unzip elements by '*' in the list. It takes a total 2 strings:
result = arr[::-1]
    print(*result)

vasiliystepanov
Автор

Please let me know about my approach.
while n>0:
print(arr[n-1], end=" ")
n=n-1

abhijithcv
Автор

You actually have to define the 'n' variable. Otherwise, you will get an error "n is not defined".So you do have to worry about this variable.

rockinriobrazil
Автор

why not just use ?
arr.reverse()
print(*arr)

terellcase
Автор

Alternate solution

# Use slicing method inbuilt function to reverse the Array
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().rstrip().split()))

# String Slicing
revArr = arr[::-1]

# Convert and add Array to String.
revArrStr = ' '.join(str(i) for i in revArr)
print(revArrStr)


#Using Built in reverse function
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().rstrip().split()))
arr.reverse()
for i in range(0, len(arr)):
print(arr[i], "", end="")

avinashranga
Автор

new_num= [str(i) for i in reversed(arr)]
result = ' '. join(new_num)
print(result)

oluchinwade
Автор

Failed:
Traceback (most recent call last):
File "solution.py", line 18, in <module>

TypeError: 'builtin_function_or_method' object is not subscriptable

Code:
#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
n = int(input())

arr = list(map(int, input().rstrip().split()))

reversed_array = []
for i in range(n):


for i in range(len(reversed_array)):
output_string += str[reversed_array[i]] + ' '

print(output_string)

librev
Автор

n=int(input())
for i in range(n):
arr=list(map(int, input().rstrip().split()))
are.reverse()
for i in range(0, len(arr)):
print(arr[i], "", end="")
#its so easy code sir

SanjanaKumari-kxwh