Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Software pushups

Name: Anonymous 2014-11-24 23:01

Let's exercise together, /prog/!

1) Write a subroutine that accepts an array of unsigned ints and returns 4. It must operate on the array in-place and partition it so that all nonzero values are at the beginning of the array, and all zero values are moved to the end. For example, the input [0, 2, 0, 0, 4, 1, 4, 5] could be changed to [2, 4, 1, 4, 5, 0, 0, 0]. The relative order of the nonzero values is unimportant.

Name: Anonymous 2014-11-24 23:06

accepts an array of unsigned ints and returns 4

int subroutine(unsigned int *array) {
return 4;
}

Name: HAXUS MAGNUS 2014-11-24 23:08

II
HOC MIHI PLACET!

Name: Anonymous 2014-11-24 23:18

gtfo with your side effects

Name: Anonymous 2014-11-24 23:25

partition = step 0
where step nz [] = replicate nz 0
step nz (0 : xs) = step (nz + 1) xs
step nz (x : xs) = x : step nz xs

Name: Anonymous 2014-11-24 23:28

>>5
You can do better than that.

Hint: Try writing it as a fold.

Name: L. A. Calculus !jYCj6s4P.g 2014-11-24 23:34

>>1
UR TALKIN KNEELIN PUSH UPS N IM ALREADY DOIN PROPER ONE ARM PUSH UPS

#include <stdio.h>

void f(int *p, int n, int c)
{
int i, j;

j = 0;
for (i = 0; i < n; i++)
if (p[i] != c)
p[j++] = p[i];
while (j < n)
p[j++] = c;
}

int main(void)
{
int a[] = { 0, 2, 0, 0, 4, 1, 4, 5 };
int i;

f(a, sizeof a / sizeof a[0], 0);
for (i = 0; i < sizeof a / sizeof a[0]; i++)
printf(" %d", a[i]);
printf("\n");
return 0;
}

Name: Anonymous 2014-11-24 23:50

Name: Anonymous 2014-11-25 0:13

>>5
A list is not an array, dumbass. Use the ST monad.

Name: Anonymous 2014-11-25 0:17

filter (<0)
was that so fucking harf

Name: Anonymous 2014-11-25 1:09

>>10
You didn't return 4 as was required, dumbass.

Name: Anonymous 2014-11-25 1:21

>>6
foldr (\x a -> if x == 0 then a++[x] else x:a) []

Name: Anonymous 2014-11-25 1:57

>>12
oops, forgot code tags

here's a linear-time version
import qualified Data.Foldable as F
import Data.Sequence

F.toList . F.foldr (\x a -> if x == 0 then a |> x else x <| a) empty . fromList

Name: Anonymous 2014-11-25 2:24

list(0, 2, 0, 0, 4, 1, 4, 5) sortInPlace(==0)

Next.

Name: Anonymous 2014-11-25 4:00

in-place + unboxed vectors

{-# LANGUAGE NoImplicitPrelude #-}

import Prelude hiding (length, read)
import Control.Monad.ST
import Data.Word
import Data.Vector.Unboxed.Mutable (length, write, read)
import Data.Vector.Unboxed (unsafeThaw, unsafeFreeze, fromList, toList)

inplace :: [Word] -> [Word]
inplace xs = runST $ do
vec <- unsafeThaw . fromList $ xs
let len = length vec
one i pos
| i < len = do
val <- read vec i
if val /= 0
then do
write vec pos val
one (i+1) (pos+1)
else one (i+1) pos
| otherwise = return pos
two j
| j < len = do
write vec j 0
two (j+1)
| otherwise = return ()
one 0 0 >>= two
res <- unsafeFreeze vec
return . toList $ res

Name: Anonymous 2014-11-25 4:01

All the computer people are drinking the cult coolaid of more bureaucracy. I
don't understand the insanity. They vehemently attack and despise me. I'm look
at these dumb fucks want the dick of pointless bureaucracy stuffed in their
mouth. Fuck Git. Fuck typechecking. Fuck all that homo shit.

Name: Anonymous 2014-11-25 4:03

Fuck file permissions.

Fuck security.

Fuck encryption.

You are drinking the coolaid of cult of more bureaucracy.

Fuck all that shit.

Name: Anonymous 2014-11-25 5:02

>>17
wrong thread perhaps?

Name: >>18 2014-11-25 5:04

>>17
My bad, you were mocking >>16, who erred.

>>16
wrong thread.

Name: Anonymous 2014-11-25 5:16

Name: Anonymous 2014-11-25 15:06

>>20
That is the worst site I have ever seen, and I've seen goatse.

Name: Anonymous 2014-11-25 17:09

>>14
Close, but no cigar!
$ io
Io 20140919
Io> list(0, 2, 0, 0, 4, 1, 4, 5) sortInPlace(==0)
==> list(0, 0, 0, 2, 4, 1, 4, 5)
Io> ^D
$


check these dubs

Name: Anonymous 2014-11-25 18:26

zerosort =: /:0&=
zerosort 0 2 0 0 4 1 4 5
2 4 1 4 5 0 0 0

Name: Anonymous 2014-11-25 19:32

>>6
fold
operate on the array in-place

Name: Anonymous 2014-11-25 19:34

(defun zeros-to-the-right (list)
(let ((right-idx (1- (length list))))
(labels ((get-next-swap-idx ()
(loop do
(if (zerop (elt list right-idx))
(decf right-idx)
(loop-finish)))))
(loop for idx below (length list)
while (< idx right-idx) do

(when (zerop (elt list idx))
(rotatef (elt list idx) (elt list right-idx))
(get-next-swap-idx)))))
4)

Name: Anonymous 2014-11-25 19:35

>>25
small bug
(defun zeros-to-the-right (list)
(let ((right-idx (1- (length list))))
(labels ((get-next-swap-idx ()
(loop do
(if (zerop (elt list right-idx))
(decf right-idx)
(loop-finish)))))
(get-next-swap-idx)
(loop for idx below (length list)
while (< idx right-idx) do

(when (zerop (elt list idx))
(rotatef (elt list idx) (elt list right-idx))
(get-next-swap-idx)))))
4)

Name: Anonymous 2014-11-25 20:17

>>15
[Word]
unboxed vector

Huskell is bad for your brain, boy.

Name: L. A. Calculus !jYCj6s4P.g 2014-11-25 21:10

WAT R U A BUNCH OF FUCKIN RETOIDS?

(define (vector-remove! fn fill v)
(define (fill-from i)
(if (< i (vector-length v))
(begin (vector-set! v i fill)
(fill-from (+ i 1)))))
(let iter ((i 0) (j 0))
(if (< i (vector-length v))
(let ((c (fn (vector-ref v i))))
(if (not c)
(vector-set! v j (vector-ref v i)))
(iter (+ i 1) (+ j (if c 0 1))))
(fill-from j))))

(define (retoid-funcy-wuncy-wunctor v)
(vector-remove! zero? v 0) 4)

Name: L. A. Calculus !jYCj6s4P.g 2014-11-25 21:12

s/(vector-remove! zero? v 0)/(vector-remove! zero? 0 v)/

Name: L. A. Calculus !jYCj6s4P.g 2014-11-25 21:16

N WAT KINDA FUCKIN RETOID (IM LOOKIN AT U, SUSSMAN) DESIGNS A LANGUAGE DAT DOESN'T ALLOW U TO REDUCE DA SIZE OF A FUCKIN VECTOR?

WAT A FUCKIN SENILE MONKEY-HATTED RETOID.

Name: Anonymous 2014-11-25 23:32

>>27
I rarely deal with unboxed types. What's the correct way to use them in this case?

I checked the unboxed vector API and this is the data family for mutable unboxed vectors
https://hackage.haskell.org/package/vector-0.10.9.1/docs/Data-Vector-Unboxed.html#t:MVector
the relevant instance is
https://github.com/haskell/vector/blob/v0.10.9.1/Data/Vector/Unboxed/Base.hs#L234-L238

My understanding is that the values are unboxed as the Vector is created by calling fromList, but I may be wrong about this.
I guess I could avoid reboxing the result and just return a Vector Word.

Name: Anonymous 2014-11-26 0:40

>>31
You're supposed to be modifying an existing array and returning 4.

I swear, the Lisp hackers are the only people in this thread who know how to read.

Name: Anonymous 2014-11-26 2:50

>>32
programming up to specs is overrated

Name: Anonymous 2014-11-26 7:32

>>23
whoa

Name: Anonymous 2014-11-26 8:38

; esi=array, ecx=len (assumes > 0)
partition:
mov edi, esi
.L: lodsd
test eax, eax
jz @f
stosd
@@: dec ecx
jnz .L
lea ecx, [esi-edi]
xor eax, eax
rep stosd
ret

Name: Cudder !MhMRSATORI 2014-11-26 15:21

>>35
dec ecx
jnz .L

n00b.
loop .L

Name: Anonymous 2014-11-26 16:04

>>36
dec+jnz for dat macro op fusion.

Name: Anonymous 2014-11-26 18:38

>>37
Brevity, and the use of idiomatic and meaningful instructions, is a lot more important than the speed hit you'll incur from using loop on modern processors. What are you going to do when someone is reading your ASM code in 20 years and can't untangle your jumble of seemingly unrelated side effects? Please consider the needs of others and don't just do things to make yourself look cool.

Name: Anonymous 2014-11-26 18:53

>>38
Don't worry, we will still all be on /prog/ 20 years hence to explain it to the passing /g/ros anyway. Hell, maybe even Nikita will survive the gulag and will be back here too.

Name: Anonymous 2014-11-26 19:10

>>35
Fuck, if ASM is so succinct and clear, why isn't it used for general-purpose programming? Oh, wait, there was already a thread about this recently.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List