Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,091 members, 7,807,264 topics. Date: Wednesday, 24 April 2024 at 11:43 AM

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

Nairaland Forum / Science/Technology / Programming / QUIZ: How Many Lines Of Code Can You Use To Reverse A List (4653 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)

QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 7:53am On Jun 04, 2017
Many languages and nethods can be used.

The aim of this quiz is to write a program that reverses a list eg [1, 2, 3] ==> [3,2,1] with minimal lines of code.

The programmer with the minimal is the winner.
In whatever language you choose, your code shall be compared to those who choose same with you.

NOTE: On reaching a semicolon ( ; ), if no line break, it is considered as line++. (if not, I know guys will use one line. grin))

Start Now. JUST FOR FUN!.

Edit:

Note:
1. Your code should show the declaration of the list
2. Method used in reversing the list.
3. Last line should print the list for confirmation.

Current List


Python Group

1. adejumoadeoluwa - 1 line
1. bjboss
1. Logicalhumour - 1 line
1. TheLordIsGr8 - 1 line (endeavour to add the missing piece)
2. kayoph - 2 lines
2. logicalhumour - 2 lines (edit)
3. bjboss - 3 lines
4. logicalhumour - 4 lines
5. TheLordIsGr8 - 5 lines

JavaScript


1. Kodejuice - 2 lines
2. Kodejuice - 3 lines

Java Group

1. DevDenky - 5 lines
2. Irelokeh - 7 lines
3. Sirnuel - 16 lines

Php Group

1. nsimageorge - 2 lines

C# Group

1. Fedup - 2 lines

2 Likes

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 10:46am On Jun 04, 2017
Is the list you will provide already sorted?

Will there be need to sort the list if it is not already sorted?

In python it's as easy as calling

input_list = sorted(input_list, reverse=False/True)
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by FrankLampard: 11:51am On Jun 04, 2017
In Java

Collections.reverse(list);
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Jenifa123: 5:37pm On Jun 05, 2017
Php


$preserved = array_reverse($input, true);
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 7:24pm On Jun 05, 2017
TheLordIsGr8:
Is the list you will provide already sorted?

Will there be need to sort the list if it is not already sorted?

In python it's as easy as calling

input_list = sorted(input_list, reverse=False/True)

No no no.
1. No need to sort anything.
2. Do not use inbuilt methods

Just create a list and maybe, use a loop or however to reverse it.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 7:25pm On Jun 05, 2017
FrankLampard:
In Java

Collections.reverse(list);

FrankLampard, Inbuilt methods NOT allowed.
Above all, you aren't topping in Java...isn't a full program in Java.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 7:28pm On Jun 05, 2017
Jenifa123:
Php

$preserved = array_reverse($input, true);
Inbuilt methods not allowed
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by logicalhumour: 7:36pm On Jun 05, 2017
Mine. Using Python
 
 1. Loli = ["Sweet", "Is"]
 2. newList = []
 3. for i in range(len(Loli)-1):
       newList += Loli.pop()
 4. print(newList)
 
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 8:25pm On Jun 05, 2017
logicalhumour is winning in Python group


4 lines
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by logicalhumour: 8:36pm On Jun 05, 2017
badthinds:
logicalhumour is winning in Python group


4 lines


grin grin grin
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 8:15am On Jun 06, 2017
# Using python 3 list comprehensions
# you can just copy this post as is into jupyter notebook or any IDE and run it to verify


from random import randint # import random integer generator

ints = [randint(x, 10*x) for x in range(10)] # create a random list of integers

# Two lines for better understanding

k = len(ints)
rev_ints = [ints[k-1-i] for i in range(k)]
print(rev_ints)




# here the same code is as a one-liner. just substitute the value of k directly in the list comprehension


rev_ints = [ints[len(ints)-1-i] for i in range(len(ints))]



# Here it is in action to reverse a list of strings

animals = ['goat', 'dog', 'cat', 'mouse']
rev_animals = [animals[len(animals)-1-i] for i in range(len(animals))]
print(rev_animals)

# If you need to change the original list all you need to do is to reassign the original list to the value of the reversed list like so

ints = rev_ints
animals = rev_animals
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 8:15am On Jun 06, 2017
badthinds:


No no no.
1. No need to sort anything.
2. Do not use inbuilt methods

Just create a list and maybe, use a loop or however to reverse it.

Check my latest post
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 8:46am On Jun 06, 2017
# Using a recursive algorithm in 4 lines to reverse a string.
# This is for string reversal only. It doesn't apply to lists in general


def reverse_string(a_str):
if a_str == '':
return a_str
return a_str[-1] + reverse_string(a_str[:-1])

print(reverse_string('nairaland'))

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:09am On Jun 06, 2017
TheLordIsGr8:
# Using python 3 list comprehensions
# you can just copy this post as is into jupyter notebook or any IDE and run it to verify


from random import randint # import random integer generator

ints = [randint(x, 10*x) for x in range(10)] # create a random list of integers

# Two lines for better understanding

k = len(ints)
rev_ints = [ints[k-1-i] for i in range(k)]
print(rev_ints)




# here the same code is as a one-liner. just substitute the value of k directly in the list comprehension


rev_ints = [ints[len(ints)-1-i] for i in range(len(ints))]



# Here it is using a list of strings

animals = ['goat', 'dog', 'cat', 'mouse']
rev_animals = [animals[len(animals)-1-i] for i in range(len(animals))]
print(rev_animals)

# If you need to change the original list all you need to do is to reassign the original list to the value of the reversed list like so

ints = rev_ints
animals = rev_animals

Please. Decide on one solution. Thanks
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:09am On Jun 06, 2017
TheLordIsGr8:
# Using a recursive algorithm in 4 lines to reverse a string



1. def reverse_string(a_str):
2. if a_str == '':
3. return a_str
4. return a_str[-1] + reverse_string(a_str[:-1])
5. print(reverse_string('nairaland'))


Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:10am On Jun 06, 2017
TheLordIsGr8 is currently second in Python group


5 lines
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:19am On Jun 06, 2017


Note:

After your answer is posted and result posted, you CAN still make an edit and repost, and results will be reposted .



Python Group

1. logicalhumour - 2 lines (edit)
2. bjboss - 3 lines
3. logicalhumour - 4 lines
4. TheLordIsGr8 - 5 lines

Java Group

Where are you guys?
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 9:47am On Jun 06, 2017
badthinds:


Please. Decide on one solution. Thanks

Is this a kind of competition or something?

I have a one-liner. But I spread it on two lines for ease of understanding for some beginners who may be here.
Also I posted another solution which is not a solution to the problem at all, but for reversing a string, which could be viewed as a subset of a list.

Ok. here is my final solution in one line using python


Given a list, x, the list could be reversed in one line like so

x = [x[len(x)-1-i] for i in range(len(x))]

print(x)
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by bjboss(m): 11:57am On Jun 06, 2017
Using python
1. def reverselist(x): return x[::-1]
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:13pm On Jun 06, 2017
TheLordIsGr8:


Is this a kind of competition or something?

I have a one-liner. But I spread it on two lines for ease of understanding for some beginners who may be here.
Also I posted another solution which is not a solution to the problem at all, but for reversing a string, which could be viewed as a subset of a list.

Ok. here is my final solution in one line using python


Given a list, x, the list could be reversed in one line like so

x = [x[len(x)-1-i] for i in range(len(x))]

print(x)

Its a quiz...just for fun.

Great. Your new code is in two lines...but you didn't declare the list in it.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 9:18pm On Jun 06, 2017
badthinds:


Its a quiz...just for fun.

Great. Your new code is in two lines...but you didn't declare the list in it.

I don't think you understand python very well.

Python doesn't do all this list declaration thing as in java, c++, fortran.
My code is still in one line which is

print([x[len(x)-1-i] for i in range(len(x))])

I am only spreading it out for ease of understanding.

Also, you won't really consider a print statement part of a code because it does nothing other than show you the result of all that has been done by the main code.
Either way, my code is a one-liner. I can break it down and explain if you'd like to see.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 9:21pm On Jun 06, 2017
bjboss is first in Python group



3 lines
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by logicalhumour: 10:04pm On Jun 06, 2017
badthinds:
bjboss is first in Python group



3 lines

Mine. 2 lines now.


1. dic = {"lst": [1,2,3], "rev": dic["lst"][::-1] }
2. print dic["rev"]
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by logicalhumour: 10:09pm On Jun 06, 2017
Really fun. grin
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Nobody: 12:25am On Jun 07, 2017
Just got here(using python):
def rev(*args):
return args[::-1]


The *args makes it possible to take as many as possible arguments in as much they are numbers....
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 12:43am On Jun 07, 2017
TheLordIsGr8:


I don't think you understand python very well.

Python doesn't do all this list declaration thing as in java, c++, fortran.
My code is still in one line which is

print([x[len(x)-1-i] for i in range(len(x))])

I am only spreading it out for ease of understanding.

Also, you won't really consider a print statement part of a code because it does nothing other than show you the result of all that has been done by the main code.
Either way, my code is a one-liner. I can break it down and explain if you'd like to see.

Sir, this is my case with the code...you're one of the smartest here to revise in one line. But in reality, whats the content of x? (do something)

Your code cannot run successfully yet.
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 12:46am On Jun 07, 2017
TheLordIsGr8:


I don't think you understand python very well.

Python doesn't do all this list declaration thing as in java, c++, fortran.
My code is still in one line which is

print([x[len(x)-1-i] for i in range(len(x))])

I am only spreading it out for ease of understanding.

Also, you won't really consider a print statement part of a code because it does nothing other than show you the result of all that has been done by the main code.
Either way, my code is a one-liner. I can break it down and explain if you'd like to see.

Sir, this is my case with the code...you're one of the smartests here to revise in one line. But in reality, whats the content of x? (do something)

Your code cannot run successfully yet.
Pardon my 'declaration', I only mean "show clearly the origin and content of your list"
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by Kodejuice: 9:52am On Jun 07, 2017
JavaScript


// 2 Lines

1. let list = [1, 2, 3];
2. for (var d=0, rev=[];d<list.length;d++)
rev[rev.length] = list[list.length - d - 1];

console.log(rev);

---------------------

// 1 Line smiley

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

console.log(rev);

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 7:14pm On Jun 07, 2017
Kodejuice:
JavaScript


// 2 Lines

1. let list = [1, 2, 3];
2. for (var d=0, rev=[];d<list.length;d++)
rev[rev.length] = list[list.length - d - 1];

console.log(rev);

---------------------

// 1 Line smiley

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

console.log(rev);




I know this may lead to arguments but I count the "console.log" line (there are ways you can do it in less lines

Anyway. 1st in Javascript with a clever code. grin

2 Likes

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by badthinds: 7:23pm On Jun 07, 2017
Kodejuice in Javascript
2 lines

1 Like

Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by webdeveloperqx: 8:25pm On Jun 07, 2017
one single line in python

print range(5,-1, -1)
Re: QUIZ: How Many Lines Of Code Can You Use To Reverse A List by bjboss(m): 9:53pm On Jun 07, 2017
badthinds:
Many languages and nethods can be used.

The aim of this quiz is to write a program that reverses a list eg [1, 2, 3] ==> [3,2,1] with minimal lines of code.

The programmer with the minimal is the winner.
In whatever language you choose, your code shall be compared to those who choose same with you.

NOTE: On reaching a semicolon ( ; ), if no line break, it is considered as line++. (if not, I know guys will use one line. grin))

Start Now. JUST FOR FUN!.

Edit:

Note:
1. Your code should show the declaration of the list
2. Method used in reversing the list.
3. Last line should print the list for confirmation.

Current List


Python Group

1. TheLordIsGr8 - 1 line (endeavour to add the missing piece)
2. kayoph - 2 lines
2. logicalhumour - 2 lines (edit)
3. bjboss - 3 lines
4. logicalhumour - 4 lines
5. TheLordIsGr8 - 5 lines

JavaScript

1. Kodejuice - 2 lines
2. Kodejuice - 3 lines

Java Group

Where are you guys?


my code has been modified now, new list please

(1) (2) (Reply)

Full-stack Developer - Full-time / Semalt Shares A Guide To Blocking And Removing Google Analytics Spam / Learn PHP & MYSQL The Fast Way.

(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. 52
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.