Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Reverse array challenge code I wrote

Posting this here looking for constructive criticism. If anyone's got any tips/advice on things I could've done better/differently, please feel free to say so.
The code challenge was:
  • Prompt: Reverse an array. A classic code interview question.

  • Difficulty: easy

  • Languages: no-restrictions

  • Constraints: The function should take in any array and return that array in reverse. If your language has a native .reverse() function cannot use it because this is a test of complexity and not how well you can use a function!

  • Bonus: Do this without allocating any extra memory. This is called modifying the array "in-place"!



Code
# reverse an array challenge

# gotta have numpy
import numpy as np

# big daddy string to get things done
def string_to_array_to_reversed_array():
   
    # for handling user input
    def user_input_function(user_input): # user_input to take in the input when called
        user_input = list(user_input.replace(" ", "")) # user_input is reassigned it's input string as a list, while user_input.replace trims any white space out
        return np.array(user_input, order = 'C') # return user_input list as an array in contiguous order to array_arg parameter in reverse_array function

    # for reversing the array
    def reverse_array(array_arg): # array_arg takes in the return from user_input_function
        return np.flipud(array_arg) # FLIPS THE ARRAY, DOESN'T REVERSE IT. YOU SEE THE .flipud part right?. NOTHING WAS SAID ABOUT FLIPPING. ONLY REVERSING.

    # x assigned to take in final return value and store it for printing
    x = reverse_array(user_input_function(input("Please type in a single word as a string: "))) # get user input
    return x # RESULTS ARE IN!

# call "main" function and make shit happen, store in reversed_array for printing.
reversed_array = string_to_array_to_reversed_array()
print(reversed_array)

I dunno if I got the bonus, although I DID get a lotta much needed experience using numpy for certain things, so at least there's that.
Anyway lemme know what you guys think.
Code
def reverse_array(normalArray):
    reversedArray = normalArray[::-1]
    return reversedArray


normalArray = [1, 2, 3, 4, 5]
reversedArray = reverse_array(normalArray)
print(reversedArray)
(Dec 1, 2020 09:07 AM)Qismon Wrote:  
Code
def reverse_array(normalArray):
reversedArray = normalArray[::-1]
return reversedArray


normalArray = [1, 2, 3, 4, 5]
reversedArray = reverse_array(normalArray)
print(reversedArray)

I would say using pythons list fucntiosm would fall under pythons built in reverse.

¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
I think what they are looking for is, loop over the array, switch first item with last item, second item with second last item etc until you reach the middle.

I know using a library isn't in their constraints, but I'm pretty sure they also mean that your thing isn't allowed because they are looking for complexity like they said
Contact me for:
- Any development (bots, checkers, homework, websites, ...)
- System administration (Linux, Windows, websites, ...)
- Cybersecurity (Reverse engineering, cracking, forensics, ...)
(Dec 1, 2020 09:27 AM)SecuroServ Wrote:  I think what they are looking for is, loop over the array, switch first item with last item, second item with second last item etc until you reach the middle.

I know using a library isn't in their constraints, but I'm pretty sure they also mean that your thing isn't allowed because they are looking for complexity like they said

Completely agree. No packages allowed.

He is my solution

PHP Code

<?php 
def reverse
(lst):
  
length = len(lst) 
  
tmp = [0 for I in range(length)] 
  for 
i in range(length):
    
idx = (length - 1) - i
    tmp
[i] = lst[idx]
  return 
tmp 

¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
This is the easiest solution I could think of. Just looping through the original array backwards and adding all the items to a new array.

PHP Code

<?php 
def reverse
(arr):
    rev = []

    for x in range(len(arr), 0, -1):
        rev.append(x)

    return rev 
[Image: 3943c24fb6058003233768a9747351d2.png]

(Dec 2, 2020 07:14 AM)L E G I Ø N Wrote:  This is the easiest solution I could think of. Just looping through the original array backwards and adding all the items to a new array.

Should it not be this?

PHP Code

<?php 
def reverse
(arr):
    rev = []

    for x in range(len(arr), 0, -1):
        rev.append(arr[x])

    return rev 

¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
Honestly it was kinda confusing for me to figure out how to go about doing this, primarily because Python doesn't technically have arrays, it has this weird kind of "lists" (I actually saw a pretty good meme about that recently) that can be a bit confusing to work with. Trying to reverse an array "in-place" (which was what I was originally wanting to do) is something I'd think is easier and more straightforward when using languages that have "normal" arrays.
This will reverse even and odd lists with almost zero memory space used, and quicker than most other solutions since it only iterates to half of the length of the list (though same big O).

PHP Code

<?php 
def rev
(lst):
    for 
i in range(len(lst) // 2):
        
lst[i], lst[len(lst) - i - 1] = lst[len(lst) - i - 1], lst[i]

    return 
lst

print(rev([1, 2, 3, 4, 5, 6, 7]))     # [7, 6, 5, 4, 3, 2, 1]
print(rev([1, 2, 3, 4, 5, 6, 7, 8]))  # [8, 7, 6, 5, 4, 3, 2, 1] 


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by jmcatena91Aug 7, 2023 04:04 AM 27 869 Aug 7, 2023 04:04 AM
Last Post: sadwweq
[C] by gurlhackerclubMay 19, 2020 05:18 PM 8 260 May 19, 2020 05:18 PM
Last Post: k1r3n
[C] by TahhApr 7, 2020 02:48 PM 4 141 Apr 7, 2020 02:48 PM
Last Post: kray