12/15/2018 - 12:20 PM


Coding Problems and solutions

# Print Array in Reverse
# Given the size and the elements of array A, print all the elements in reverse order.

# Input: 
# First line of input contains, N - size of the array. 
# Following N lines, each contains one integer, i{th} element of the array i.e. A[i].

# Output: 
# Print all the elements of the array in reverse order, each element in a new line.

rawInput = raw_input()
arr = []
for each in range(int(rawInput)):
for value in reversed(arr):
    print value

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index,  (where ), that can be referenced as  (you may also see it written as ).

Given an array, , of  integers, print each element in reverse order as a single line of space-separated integers.


import math
import os
import random
import re
import sys

# Complete the reverseArray function below.
def reverseArray(a):
    return reversed(a)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr_count = int(raw_input())

    arr = map(int, raw_input().rstrip().split())

    res = reverseArray(arr)

    fptr.write(' '.join(map(str, res)))

# They are accessed as list.method(). Some of the methods have already been used above.

# Python List Methods
append() - Add an element to the end of the list
extend() - Add all elements of a list to the another list
insert() - Insert an item at the defined index
remove() - Removes an item from the list
pop() - Removes and returns an element at the given index
clear() - Removes all items from the list
index() - Returns the index of the first matched item
count() - Returns the count of number of items passed as an argument
sort() - Sort items in a list in ascending order
reverse() - Reverse the order of items in the list
copy() - Returns a shallow copy of the list

Built-in Functions with List
Function	Description
all()	Return True if all elements of the list are true (or if the list is empty).
any()	Return True if any element of the list is true. If the list is empty, return False.
enumerate()	Return an enumerate object. It contains the index and value of all the items of list as a tuple.
len()	Return the length (the number of items) in the list.
list()	Convert an iterable (tuple, string, set, dictionary) to a list.
max()	Return the largest item in the list.
min()	Return the smallest item in the list
sorted()	Return a new sorted list (does not sort the list itself).
sum()	Return the sum of all elements in the list.