loading...

DAY 7: ARRAYS

idimaimuna profile image M__ ・1 min read

The challenge was focusing on arrays in Java but since I am learning python, I call them data structures and Python has various data structures such as lists, sets, tuples and dictionaries.

Task:
Given an array,A , of N integers, print A's elements in reverse order as a single line of space-separated numbers.
The first line of the input contains an integer, N (array size).
The second line of the input contains N space-separated integers describing array A's elements.

# input the size of your list as an integer
n = int(input())

#user inputs the values
#strip() removes any leading or trailing whitespace
#split(' ') separates the values using space
#the values are then added to the arr list

arr = list(int(i) for i in input().strip().split(' '))

#loop through the reversed list and print the output 
for num in reversed(arr):
    print(num, end=' ')

'''
Sample Input:
4
1 4 3 2

Sample Output:
2 3 4 1
'''

I struggled a bit with this but I managed to get through it and learn even more about list comprehensions (more compact way to create lists without writing many lines of code). More practice is definitely needed so I am going to try and apply this technique more often so that I can familiarize myself with it better.

Posted on by:

idimaimuna profile

M__

@idimaimuna

New programmer interested in web development and python as well. Aspiring to learn and be better than I started.

Discussion

markdown guide