Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,260 members, 7,818,887 topics. Date: Monday, 06 May 2024 at 07:23 AM

QUIZ: How Many Lines Of Code Can You Use To Reverse A List - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / QUIZ: How Many Lines Of Code Can You Use To Reverse A List (4679 Views)

30 Days Of Code {April 19 - May 18} / 100 Days Of Code Challenge / How Many Lines Of Code Run Nairaland? (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Kodejuice: 8:25am On Jun 08, 2017


//JS 1 Line wink

1. list=[1, 2, 3],
console.log( rev = list.reduce((x, y, i)=>x.concat(list[list.length - i - 1]), []) );

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by antieverything(m): 11:38am On Jun 08, 2017
bash shld do the trick
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by orimion(m): 10:16pm On Jun 08, 2017
Two lines

// JavaScript with some ES6 and recursion trick wink

const list = ['a','b',3,NaN,undefined, 'i'],
reverse = ([first, ...rest]) =>
rest.length > 0 ?
[...reverse(rest), first]
:
[first]
;

console.log(reverse(list))



// One line with an IIFE thrown in the mix

console.log((function reverse ([first, ...rest]) {
return (
rest.length > 0 ?
[...reverse(rest), first]
:
[first]) }
)
(['a','b',3,NaN,undefined, 'i'])
);

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Sirnuel: 2:38pm On Jun 10, 2017
//////JAVA


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

public static void main(String[] args) {
// write your code here

List<Integer> myList= new ArrayList<Integer>(Arrays.asList(9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4));
List<Integer> reverseList = new ArrayList<>();

for(int i = myList.size()-1; i >= 0 ; --i){
reverseList .add(myList.get(i));
}

//this line dosent count either it is used to print out the reversed list
System.out.println(reverseList);

}
}
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by nsimageorge(m): 7:26pm On Jun 11, 2017
This is interesting...

In PHP

$array = array(1, 2, 3);
for($i=sizeof($array)-1; $i>=0; $i--){echo $array[$i];}

2lines grin grin
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:57pm On Jul 05, 2017
webdeveloperqx:

one single line in python

print range(5,-1, -1)
Nice one...but your code didn't show clearly what you reversed. Ie. Where is the reversed one?
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 12:32pm On Jul 06, 2017
3 Lines with C#

List<int> yourList = new List<int>() {1,2,3,4,5};
yourList.Reverse();
yourList.ForEach(items => Console.Write(items));
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 2:05pm On Jul 06, 2017
Febup:
3 Lines with C#
List<int> yourList = new List<int>() {1,2,3,4,5}; yourList.Reverse(); yourList.ForEach(items => Console.Write(items));
Clean code, but you used inbuilt ".Reverse()". Not allowed.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 6:58pm On Jul 06, 2017
Take this: now 2 Lines with C#

List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
for (int i = (list.Count - 1); i >= 0; i--){Console.WriteLine(list.ToArray()[i].ToString());}
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Sync201(m): 11:42pm On Jul 06, 2017
#python just 1lines
L=['a','b','c','d'] ; print(L[::-1])


<<<Output>>>
['d','c','b','a']
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 6:38pm On Jul 08, 2017
Sync201:
#python just 1lines
L=['a','b','c','d'] ; print(L[::-1])


<<<Output>>>
['d','c','b','a']

grin grin grin

Lol. If you read the first post...after semicolon...like the one you used, its a line++
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 9:58pm On Jul 08, 2017
At op. Java code is 4 lines, you need to ignore the framework.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 8:45am On Jul 09, 2017
Febup:
At op. Java code is 4 lines, you need to ignore the framework.

I know, but what we want is something anyone can copy and run successfully. Its possible to do it without all those inclusion
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 11:48am On Jul 09, 2017
badthinds:


I know, but what we want is something anyone can copy and run successfully. Its possible to do it without all those inclusion

In that case you will need to add 9 more lines to JavaScript as you cannot run JavaScript and display your output without the lines below. This will also apply to all the other languages as you will have to make some language declarations before you can run your code.

<HTML>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<div> </div>
</body>
</html>
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 5:04pm On Jul 09, 2017
Febup:


In that case you will need to add 9 more lines to JavaScript as you cannot run JavaScript and display your output without the lines below. This will also apply to all the other languages as you will have to make some language declarations before you can run your code.

<HTML>
<head>
<script type="text/javascript">
</script>
</head>
<body>
<div> </div>
</body>
</html>


How about the nodeJs console grin

Lol. The examples you gave, eg in the case of javascript, all those html,body stuffs ain't necessary (the <script> is only necessary if its gonna be executed by a browser)...above all, they are not part of Javascript.


But anyway, all those with....eg java codes, same principles would be followed in ranking...so, it is fair.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 7:59pm On Jul 09, 2017
badthinds:

How about the nodeJs console grin

Lol. The examples you gave, eg in the case of javascript, all those html,body stuffs ain't necessary (the <script> is only necessary if its gonna be executed by a browser)...above all, they are not part of Javascript.

OK in that case you'll need to add 7 more lines for JavaScript.

<HTML>
<head>
<script type="text/javascript">
/* Your code goes hear */

alert('Your Results');
</script>
</head>
</html>
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by orimion(m): 3:37am On Jul 10, 2017
Febup:


OK in that case you'll need to add 7 more lines for JavaScript.

<HTML>
<head>
<script type="text/javascript">
/* Your code goes hear */

alert('Your Results');
</script>
</head>
</html>
Its like you didn't read it well, he said
badthinds:

How about the nodeJs console grin

Lol. The examples you gave, eg in the case of javascript, all those html,body stuffs ain't necessary (the <script> is only necessary if its gonna be executed by a browser)...above all, they are not part of Javascript.
all those tags are not part of Javascript, they are HTML just fire up your browser and go to console in the dev tool or better still the nodejs repl

1 Like

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 11:11am On Jul 10, 2017
orimion:
Its like you didn't read it well, he said

My bad.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by adejumoadeoluwa(m): 7:44am On Jul 11, 2017
reverse = lambda x: x[::-1]


#one liner bitch!
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by bjboss(m): 3:54pm On Jul 25, 2017
bjboss:
Using python
1. def reverselist(x): return x[::-1]




i think you av to update the list>>
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Irelokeh(m): 5:27pm On Jul 25, 2017
Here's mine with JAVA


import java.util.*;

public class Main
{
public static void main(String[] args)
{
int set [] = {1,2,3,4,5,8,5,5};
int a = set.length;
for (int i=0; i<=set.length-1; i++) {
System.out.println(set[a-=1]);
}
}
}
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 10:42am On Jul 27, 2017
adejumoadeoluwa:
reverse = lambda x: x[::-1]


#one liner bitch!
Nice one Motherfcuker! grin
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 10:46am On Jul 27, 2017
bjboss:





i think you av to update the list>>
LOL. I was looking at the code and grinding my teeth...but there was nothing I could hold against it being technically 1 line or not. grin

1 Like

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by DevDenky: 6:33am On Jul 28, 2017
Import Java.util.*;

public class Main{
public static void main(String[] args) {


List<Integer> list = Arrays.asList(1,2,3,4);

for(int x=list.size() ; x>0; x- -)
System.out.println(list[x]);
}
}

1 Like

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by harryobas: 10:48am On Aug 14, 2017
In Ruby:

def reverse_list(list)
idx = -1
reversed_list = []
list.length.times {reversed_list << list[idx]; idx -= 1}
reversed_list
end
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by ACFreeHost: 11:11pm On Oct 08, 2017
Are you a website developer or a web designer and you need a reliable hosting server provider to host your website for FREE and Adverts FREE? That's why ACFreeHost is here to make that possible for you Publishers, just all you need is to visit www.acfreehost.com and register then you are good to go. No Adverts on your sites, No disk limite, No bandwidth limite e.t.c. Don't worry all are FREE and if you have any question their pleased to assist you. So visit www.acfreehost.com and register Now.!!



VISIT : [url]WWW.ACFREEHOST.COM[/url]

(1) (2) (Reply)

Learn PHP & MYSQL The Fast Way. / Creating A New Face For Nl / Frontendmasters.com Courses Over 315.75GB

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.