Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,386 members, 7,815,821 topics. Date: Thursday, 02 May 2024 at 06:53 PM

Simple Algorithm Exercise - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Simple Algorithm Exercise (3728 Views)

Algorithm Study Plan / Thread For Nairaland Algorithm Questions / Nigerian Developers How Did You Master Algorithm And Problem Solving (2) (3) (4)

(1) (Reply) (Go Down)

Simple Algorithm Exercise by frankg1(m): 8:18pm On Sep 24, 2017
Once upon a time, on a way through the old wild west,…

… a man was given directions to go from one point to another. The directions were "NORTH", "SOUTH", "WEST", "EAST". Clearly "NORTH" and "SOUTH" are opposite, "WEST" and "EAST" too. Going to one direction and coming back the opposite direction is a needless effort. Since this is the wild west, with dreadfull weather and not much water, it's important to save yourself some energy, otherwise you might die of thirst!

How I crossed the desert the smart way.

The directions given to the man are, for example, the following:

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"].
or

{ "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" };
or (haskell)

[North, South, South, East, West, North, West]
You can immediatly see that going "NORTH" and then "SOUTH" is not reasonable, better stay to the same place! So the task is to give to the man a simplified version of the plan. A better plan in this case is simply:

["WEST"]
or

{ "WEST" }
or (haskell)

[West]
or (rust)

[WEST];
Other examples:

In ["NORTH", "SOUTH", "EAST", "WEST"], the direction "NORTH" + "SOUTH" is going north and coming back right away. What a waste of time! Better to do nothing.

The path becomes ["EAST", "WEST"], now "EAST" and "WEST" annihilate each other, therefore, the final result is [] (nil in Clojure).

In ["NORTH", "EAST", "WEST", "SOUTH", "WEST", "WEST"], "NORTH" and "SOUTH" are not directly opposite but they become directly opposite after the reduction of "EAST" and "WEST" so the whole path is reducible to ["WEST", "WEST"].

Task

Write a function dirReduc which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).

The Haskell version takes a list of directions with data Direction = North | East | West | South. The Clojure version returns nil when the path is reduced to nothing. The Rust version takes a slice of enum Direction {NORTH, SOUTH, EAST, WEST}.

Examples

dirReduc({"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"}) => {"WEST"}
dirReduc({"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"}) => {}
See more examples in "Example Tests"

Note

Not all paths can be made simpler. The path ["NORTH", "WEST", "SOUTH", "EAST"] is not reducible. "NORTH" and "WEST", "WEST" and "SOUTH", "SOUTH" and "EAST" are not directly opposite of each other and can't become such. Hence the result path is itself : ["NORTH", "WEST", "SOUTH", "EAST"].

1 Like

Re: Simple Algorithm Exercise by nsimageorge(m): 10:02pm On Sep 24, 2017
Using JavaScript

function dirReduc(arr){
var string = arr.join( " " );
var wrongMatch = /NORTHSOUTH|SOUTHNORTH|EASTWEST|WESTEAST/;
while(wrongMatch.test(string)) {
string = string.replace(wrongMatch, '');
}
return string.match(/NORTH|SOUTH|EAST|WEST/) || [];
}
Re: Simple Algorithm Exercise by yorex2011: 9:54pm On Sep 25, 2017
Isnt this a case of just removing pairs of opposing directions till you are left with one or none...?
Re: Simple Algorithm Exercise by frankg1(m): 10:43pm On Sep 25, 2017
nsimageorge:
Using JavaScript

function dirReduc(arr){
var string = arr.join( " " );
var wrongMatch = /NORTHSOUTH|SOUTHNORTH|EASTWEST|WESTEAST/;
while(wrongMatch.test(string)) {
string = string.replace(wrongMatch, '');
}
return string.match(/NORTH|SOUTH|EAST|WEST/) || [];
}

Your code didnt pass it... sorry
Re: Simple Algorithm Exercise by nsimageorge(m): 7:52pm On Sep 26, 2017
frankg1:


Your code didnt pass it... sorry

https://codepen.io/nsimageorge/pen/VMpeVB

This is my solution on code pen...
How did it not pass??
Re: Simple Algorithm Exercise by frankg1(m): 8:27pm On Sep 26, 2017
nsimageorge:


https://codepen.io/nsimageorge/pen/VMpeVB

This is my solution on code pen...
How did it not pass??

run it here https://www.codewars.com/kata/directions-reduction/train/javascript.

that is where i got the question from
Re: Simple Algorithm Exercise by nsimageorge(m): 8:50pm On Sep 26, 2017
frankg1:


run it here https://www.codewars.com/kata/directions-reduction/train/javascript.

that is where i got the question from
Okay, I see my mistake

function dirReduc(arr){
var string = arr.join( '' );
var wrongMatch = /NORTHSOUTH|SOUTHNORTH|EASTWEST|WESTEAST/;
while(wrongMatch.test(string)) {
string = string.replace(wrongMatch, '');
}
return string.match(/NORTH|SOUTH|EAST|WEST/g) || [];
}
Re: Simple Algorithm Exercise by orimion(m): 7:23pm On Sep 27, 2017
Haskell

dirReduce :: [Direction] -> [Direction]
dirReduce xs =
case reduce 0 [] xs of
(0, ds) -> reverse ds
(_, ds) -> dirReduce $ reverse ds

reduce :: Int -> [Direction] -> [Direction] -> (Int, [Direction])
reduce t acc [] = (t, acc)
reduce t acc (North:South:ds) = reduce (t + 1) acc ds
reduce t acc (South:North:ds) = reduce (t + 1) acc ds
reduce t acc (West:East:ds) = reduce (t + 1) acc ds
reduce t acc (East:West:ds) = reduce (t + 1) acc ds
reduce t acc (d:ds) = reduce t (d : acc) ds
Re: Simple Algorithm Exercise by escapefromusa(f): 1:51am On Sep 28, 2017
Not clear what to do.

Do we . Take a dir.. like go north, then we can't got north again?
Re: Simple Algorithm Exercise by deedat205(m): 11:24am On Sep 29, 2017
orimion and Haskell are 5 and 6

def dirReduc(l):
cor = ['NORTH','SOUTH','WEST','EAST']
i = 0
while i < len(l)-1:
if l[i] == cor[0] and l[i+1] == cor[1] or l[i] == cor[1] and l[i+1] == cor[0]:
del(l[i])
del(l[i])
i = 0
elif l[i] == cor[2] and l[i+1] == cor[3] or l[i] == cor[3] and l[i+1] == cor[2]:
del(l[i])
del(l[i])
i = 0
else:
i += 1
return l
Re: Simple Algorithm Exercise by frankg1(m): 9:05am On Sep 30, 2017
orimion:
Haskell

dirReduce :: [Direction] -> [Direction]
dirReduce xs =
case reduce 0 [] xs of
(0, ds) -> reverse ds
(_, ds) -> dirReduce $ reverse ds

reduce :: Int -> [Direction] -> [Direction] -> (Int, [Direction])
reduce t acc [] = (t, acc)
reduce t acc (North:South:ds) = reduce (t + 1) acc ds
reduce t acc (South:North:ds) = reduce (t + 1) acc ds
reduce t acc (West:East:ds) = reduce (t + 1) acc ds
reduce t acc (East:West:ds) = reduce (t + 1) acc ds
reduce t acc (d:ds) = reduce t (d : acc) ds


i dont understand this haskell one bit... but it works though
Re: Simple Algorithm Exercise by orimion(m): 3:41pm On Sep 30, 2017
deedat205:
orimion and Haskell are 5 and 6

def dirReduc(l):
cor = ['NORTH','SOUTH','WEST','EAST']
i = 0
while i < len(l)-1:
if l[i] == cor[0] and l[i+1] == cor[1] or l[i] == cor[1] and l[i+1] == cor[0]:
del(l[i])
del(l[i])
i = 0
elif l[i] == cor[2] and l[i+1] == cor[3] or l[i] == cor[3] and l[i+1] == cor[2]:
del(l[i])
del(l[i])
i = 0
else:
i += 1
return l

cheesy don't you just love how it reads
frankg1:



i dont understand this haskell one bit... but it works though
grin http://learnyouahaskell.com it'll challenge everything you know about programming

(1) (Reply)

Contest[closed] : Program A Function to find if a phrase. is in a string / What U Need To Know Before Learn A Programming Language / What The Different Between ENUM And ARRAY In Java

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