Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 1 // Arrays and Strings #47

Open
profnandaa opened this issue Sep 27, 2021 · 30 comments
Open

Session 1 // Arrays and Strings #47

profnandaa opened this issue Sep 27, 2021 · 30 comments

Comments

@profnandaa
Copy link
Contributor

Challenge: Write a function/method ReverseArray(a) that takes an array of integers and reverses it.

e.g.

[1, 4, 5, 6] -> [6, 5, 4, 1]
@MavineRakiro
Copy link

Challenge: Write a function/method ReverseArray(a) that takes an array of integers and reverses it.

e.g.

[1, 4, 5, 6] -> [6, 5, 4, 1]

https://replit.com/@MavineRakiro/ReverseArray#main.py

@EssyK
Copy link

EssyK commented Sep 27, 2021

@Consolata-max
Copy link

@Devabubakar
Copy link

@edgarmokua
Copy link

@cyenite
Copy link

cyenite commented Sep 27, 2021

@JayKay24
Copy link

Should we modify the input array? I didn't.
https://replit.com/@JamesKinyua/ReverseArray#index.ts

@bmwasonga
Copy link

@Antony-Ndungu
Copy link

@leonardsangoroh
Copy link

static int ReverseArray(int [] nums) {

    int length = nums.length - 1;
    
    int [] newArr = new int [length];
    
    int k = 0;
    for (int i = length; i>=0; i--) {
        
        newArr [k] = nums[i];
        k++;
    }
    
    return newArr.length;
}

@samuelkamotho92
Copy link

@agesa3
Copy link

agesa3 commented Sep 27, 2021

@teddbot
Copy link

teddbot commented Sep 27, 2021

@Mburia
Copy link

Mburia commented Sep 27, 2021

@Shelton17
Copy link

@RonnyK36
Copy link

var a = [1,2,3,4,5,6,7]
function ReverseArray(a) {
var arr1 = new Array;
for(var i = a.length-1; i >= 0; i--) {
arr1.push(a[i]);
}
return arr1;
}

var b = ReverseArray(a);
console.log(b);

@RonnyK36
Copy link

@Brian-Weloba
Copy link

@Milgo21
Copy link

Milgo21 commented Sep 27, 2021

@JennyRJ
Copy link

JennyRJ commented Sep 27, 2021

// Done in JavaScript

let a = [1,2,3,4,5,6,7]
function ReverseArray(a) {
let reversed = new Array;
for(var i = a.length-1; i >= 0; i--) {
reversed.push(a[i]);
}
return reversed;
}
let b = ReverseArray(a);
console.log(b);

@aayushisingh11
Copy link

@Dancanmilgo73
Copy link

@enjuguna
Copy link

enjuguna commented Sep 28, 2021

https://replit.com/@enjuguna/BarrenAgonizingSales#index.js

Code:

function ReverseArray(a)
{ 
  arr = []
  for (var i = a.length - 1; i >= 0; i--) {
  arr.push(a[i])
}     
}
var a =  [1, 2, 3, 4, 5];
console.log("Initial Array", a);
ReverseArray(a);
console.log("Final Array ",arr) 

Output:

Initial Array [ 1, 4, 6, 5, 3 ]
Final Array  [ 3, 5, 6, 4, 1 ]

@kwaru
Copy link

kwaru commented Sep 29, 2021

/**

  • Java method reverseArray
  • takes an array of integers and reverses it.
    *e.g.
    *[1, 4, 5, 6] -> [6, 5, 4, 1]
    **/

static void reverseArray( int []a){

  if(a.length==0)                                 // check if the array is  empty do nothing
      return;

    for(int i=a.length-1; i>=0; i--) {         // if not  print elements in reversed order

        if(i==a.length-1) {
            System.out.print("[ " + a[i]);
        }
        else {
            System.out.print("," + a[i]);
        }
    }
    System.out.print("]");

}

@JosephMagiya
Copy link

def reverseArray(A: str) -> str:
    if len(A) <= 1:
        return A
    left = 0
    right = len(A)-1

    while left < right:
        A[left], A[right] = A[right], A[left] # These have to be done in the same line
        left += 1
        right -= 1

    return A

reverseArray([10, 5, 6, 9])
[9, 6, 5, 10]

@manurotich
Copy link

using System;

namespace reverseArrays
{
class Program
{
static void Main(string[] args)
{
var arr = new [] {10, 5, 6, 9};
var result = new int[arr.Length];

        for(int i = arr.Length - 1; i >= 0; i--)
        {
            result[arr.Length-i-1] = arr[i];
        }

        foreach(var item in result)
        {
             Console.WriteLine(item);
        }    
    }
}

}

@kelvinbe
Copy link

kelvinbe commented Oct 5, 2021

const reverseArray = (a) => {
let b = []

for(let i = a.length-1; i>=0; i--){
    b.push(a[i]);

}
console.log(b)

}

reverseArray([1,2,3,4,5])

@JuneMuoti
Copy link

@ebarasa
Copy link

ebarasa commented Oct 14, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests