Code challenge time!

Code challenge time!

>Given an arbitrary integer and an arbitrary base, write a program that expresses the digits in list/array form
>You cannot use loops
>You cannot use String parsing, the integers must stay as integers throughout the program

go

Attached: kids standing on lawn being dumb.jpg (378x277, 17K)

I'm sorry user, we've decided to pursue other candidates.

> please do my homework

I already know the answer you poo in loo

AP classes are really lowering their standards, huh?

school aint in session dingus

x=[input("throw me a number cuz op is gæ)]
y=[input("throw me another number cuz op is gæ)]
x=x.tostr
x=[]y.append

there you go, a mix of ruby and python.

You must be 18+ to post here.
Mods, do your job.

Honestly it would be more entertaining to try to watch Jow Forums solve this with loops and string parsing.

>>You cannot use loops
Not possible. If a list/array of the digits needs to be constructed then a loop is necessary. A comprehension counts as a loop.

>wut is rekursion??

Attached: 1481002858463.gif (480x292, 3.48M)

>a loop

Recursion is a kind of loop. There's no way to do it without loops

static List toBaseN(BigInteger num,BigInteger numBase)
{
if(numBase>num)
{
var res = new List();
res.Add(num);
return res;
}
else
{
var res = toBaseN(num / numBase, numBase);
res.Add(num % numBase);
return res;
}
}

>Given an arbitrary integer and an arbitrary base, write a program that expresses the digits in list/array form
what the fuck does this mean? do you speak english? The way you wrote this, all you are asking is for the program to create an array with a length of two, which contains the two input digits. Do you want to try again at explaining this?

>var res = toBaseN(num / numBase, numBase);
>recursive call
This code uses loops, dropped

A length of two was never specified in any way

A real question should show an example.
I believe what OP wants is something like this.
input 22
output [2, 2]
But he wants a base conversion? It says to accept an arbitrary base but it doesn't say to do a base conversion.

Basically, this problem is stupid and poorly defined.

No base conversion is necessary or need be assumed. You can solve the problem without any kind of conversion

Or rather, you would be able to, if not for the fact that recursion is a kind of loop and therefore isn't allowed, or at least shouldn't be allowed or else OP is a retard

this, i don't understand the question past simply taking two digits and printing them as a list/array, but that doesn't sound right

where are you getting this "two digits" idea

>given an arbitrary integer and an arbitrary base
these are the only inputs specified

what's giving you the idea that each one has to be one digit

two integers, not two digits, i mean

But then why does it ask for an arbitrary base input?

so you think the problem is to take two integers and print them as a list/array?
no, not quite.
do you understand what a "base" is in this context?
does the word "radix" ring a bell?
a "base" is the number of distinct digits in a number system. for example, our decimal number system is "base" ten. binary, on the other hand, is "base" two. hexadecimal is "base" sixteen.
the desired function would take an integer and another integer to be the first integer's "base." i.e., if the second integer is sixteen, the first integer must be treated as hexadecimal when breaking it up into digits. if ten, it must be treated as decimal; if two, binary. et cetera.
even as the first integer stays the same, the number of digits it must be broken up into varies with the base.
because that will affect the numerical values of the digits you get back in the array. you don't have to convert the input integer to/from that base first, you just have to treat it as that base. see for example

i know what a base is, but the question doesn't say to do anything with it, and it doesn't say to do anything with the integers besides "expressing" them

it's obvious to any non-turboautist what the question meant. contrarianism is a stupid philosophy

maybe me and are just dumbasses, because it wasn't clear to us
leave room for alternative interpretations, and expect weird answers, this goes for programming as well as human communication

>30 replies bitching about the question
>one solution
Never change, Jow Forums

i'm not OP tho

i didn't say you were op

NotALoop:
//your code here
if (!i[y] = '/o')
goto NotALoop;

>the desired function would take an integer and another integer to be the first integer's "base." i.e., if the second integer is sixteen, the first integer must be treated as hexadecimal when breaking it up into digits.
You see, THIS was not specified in the challenge.
It could have just as easily been the output's base.

An incorrect solution.

you implied it by telling me to leave room for alternative interpretations (of OP's problem statement, which i didn't write) and expect weird answers (to OP's problem statement which i didn't write)
that's a loop
you can infer it from the second constraint:
>You cannot use String parsing, the integers must stay as integers throughout the program

Actually, that's NotALoop

NotALoop is a loop

because the nouns are not plural, they are singular, so since there are two of them, the input is only two individual digits.

...

hard to answer a question when it's asked in a way that makes no sense.

>dont use loop for obvious loop-problem
Do your own homework, Tyrone.

What digits are we expressing in list form?

Obviously the digits that result from considering the arbitrary integer as represented in the arbitrary base

The value of an integer doesn't depend on its base. OP specifically asked to not treat an integer as a string, therefore it has to be parsed as an integer first, performing the opposite of what he wants. After getting the value of an intger it's simply a matter of getting digits one by one by using division/remainder on the value of that integer.

"the digits that result from considering"?

>OP specifically asked to not treat an integer as a string, therefore it has to be parsed as an integer first,
Wrong, the integer is already an integer. No parsing is necessary.
Yes

>Wrong, the integer is already an integer.
Even simpler then, just go straight to the division by base.

But you have to do it one by one. And you can't do anything "one by one" without some kind of loop. Ergo the problem as stipulated is unsolvable

So a base conversion.

what you are saying is oxymoron, how can NotALoop be a loop?

Just unroll the loop, bro.

I'm going to guess that because he didn't say "without the use of recursion", answers like are fine, because it uses recursion without a loop (for/while) statement.

No because you're not converting from anything. The integer, as well as the base itself, are pure numeric values. They admit no inherent string representation. They admit no innate base.
>because it uses recursion without a loop
But recursion is a loop.
Analytical consequence of direct observation
Unrolling the loop is a static operation and requires knowledge of the arguments at compile / preprocess time. We might not have this knowledge at that time.

>Unrolling the loop is a static operation and requires knowledge of the arguments at compile / preprocess time.
OP never said the arguments have to be provided at runtime.

If you're using as an example of the code done right, then your explanation is wrong.

That code converts base 10 input to the arbitrary base. The input integer is not "treated as" the input base, it is converted to the input base. The input integer is only treated as base 10.

what he means, is that you have to know how much to unroll before unrolling, you can't write catch em all unroll without a loop.

>But recursion is a loop.
A loop is recursion. Recursion is not a loop.

>No because you're not converting from anything. The integer, as well as the base itself, are pure numeric values. They admit no inherent string representation. They admit no innate base.
You have to be converting from or to something, you double nigger.

To give credit where credit is due, he did technically say, "GIVEN _____, WRITE A PROGRAM _____." Technically this entails that before we even write the program, the arguments are already givens. So, going by the letter of the law, as it were, unrolling the loop is a valid solution. But I don't think that's what OP meant.
Where is the input ever treated as base 10?

>That code converts base 10 input to the arbitrary base
Wrong. It COULD work as converting if you PARSED the integer in a different base, but that's merely because by parsing we get the value of an integer, and that doesn't depend on the base.

>Recursion is not a loop.
Yes it is, it's a situation where you jump back to a prior instruction within the same subroutine. That's a loop.
>You have to be converting from or to something, you double nigger.
If the number you're treating as being in a certain base isn't in any particular base before you treat it as such, but is just a pure number without base, then you're not converting from any base.

>Where is the input ever treated as base 10?
Plug any number in and check.
It'll only work if the input is treated as base 10 and you're converting to the arbitrary base.

>Plug any number in and check.
What about 0x1FA?

It only works as base 10 becuase you're parsing it as base 10.

>Yes it is, it's a situation where you jump back to a prior instruction within the same subroutine.
That's a loop, but is recursion that doesn't use a loop. It doesn't jump back to a prior instruction in the same subroutine but still does recursion.

>It doesn't jump back to a prior instruction in the same subroutine
Yes it does:
>var res = toBaseN(

Syntax error.

Then replace it with whatever equivalent format whatever language that is supports for hex numbers.

That's running an entirely new subroutine, doofus.

Retard, read the question properly. You can't parse them as strings.

No, it's running the same subroutine in what may or may not be a new stack frame.

uhh, can't think of any solution without having any "loop"

Either way, it's not a loop, but it is recursion.

>try to make a nice thread encouraging anons to think recursively
>retard spergs out over the word "loop" for a literal hour and a fucking half
>is technically right but obviously understood what the question really meant and refuses to answer the intended question
>meanwhile admonishes other anons for being technically right but deliberately refusing to answer the intended question in exactly the same way
>one (1) real solution
wtf :(

>Either way, it's not a loop
Yes it is, it jumps back to a prior instruction in the same subroutine. Namely the start of it, while (possibly) pushing a stack frame.

The big question is how you input the values, I think.
If you input 327 and 8 you'll get [5,0,7] as output. If you input 0327 and 8, you might get [0,3,2,7] out, or 0327 and 010.
For base 16 and 0x1FA, the same. If you just put in 1FA you'll syntax error, if you put in 0x1FA and 16 maybe it'll work but 0x1FA and 0x10 would probably work properly, at least theoretically.

I got like half way there in lisp then got pretty annoyed with doing it without any looping. In languages designed to prefer functional recursion this is pretty simple but it's a pain in the ass for normal languages meant to use control flow standard like for loops

But it doesn't. It's running the subroutine again, a new instance of the same.

I don't even use functional languages on regular basis, just Rust and TS and I already hate using loops. Reducing, mapping, iterators etc are just much more concise. Loops seems limited and bloated, I rather try to calculate everything in a "monad".

just use recursion kid.

Tell your professor that this assignment makes no sense

defmodule Digitizer do
defmodule Digitizer do
@spec to_digits(integer, integer) :: [integer, ...]
def to_digits(int, base \\ 10), do: do_digits(int, base, [])

defp do_digits(int, base, acc) when int < base, do: [int | acc]
defp do_digits(int, base, acc), do: do_digits(div(int, base), base, [rem(int, base) | acc])
end

defmodule DigitizerTest do
use ExUnit.Case

describe "to_digits/2" do
test "takes a base-10 integer and returns its digits" do
assert to_digits(321) == [3, 2, 1]
end

test "takes a base-16 integer and returns its digits" do
assert to_digits(0xABC) == [0xA, 0xB, 0xC]
end
end
end

ignore the double-defmodule at the start

This is why Jow Forumstards cant get anything above basic IT-janitor jobs.

we're not going to do your homework for you user. you should actually be learning.

This is what happens when you make a poorly defined problem and you present it to programmers.
Constraints and definitions are important.

#include
#include
#include

int constructArray(int num, int base);
void newDigit(int num, int base, int position, int *output);

int main()
{
int num=0, base=0, *digits;
printf("number?");
scanf("%d",&num);
printf("base?");
scanf("%d",&base);
digits=constructArray(num,base);
};

int constructArray(int num, int base){
int length;
length=floor(log(num)/log(base))+1;
int output[length];
newDigit(num,base,length,output);
return output;
};

void newDigit(int num, int base, int position, int *output){
if(num!=0){
newDigit(floor(num/base), base, (position-1), output);
output[position-1]=num%base;
printf("%d, ",num%base);
};
}

using recursion, most significant digit into v[0]
fn digits(mut n: u32, base: u32) -> u32 {
n /= base;
if n > 0 {
1 + digits(n, base)
} else {
1
}
}

fn push_digits(n: u32, base: u32, mut i:u32, store: &mut Vec) {
if i > 0 {
i -= 1;
store.push(n / base.pow(i) % base);
push_digits(n, base, i, store);
}
}

fn to_digits(n: u32, base: u32) -> Vec {
let d = digits(n, base);
let mut v = Vec::with_capacity(d as usize);
push_digits(n, base, d, &mut v);
v
}

pub fn main() {
println!("{:?}", to_digits(12345, 10));
println!("{:?}", to_digits(0b101, 2));
println!("{:?}", to_digits(0xFF, 16));
}

I was able to convert my Elixir solution () to an LFE solution:
(defmodule digitizer
(export (digits 1) (digits 2))
)
(defun digits (int base) (do_digits int base ()))
(defun digits (int) (do_digits int 10 ()))

(defun do_digits
((int base acc) (when (< int base)) (cons int acc))
((int base acc) (do_digits (div int base) base (cons (rem int base) acc)))
)

It wasn't clear what you wanted

Attached: digits.png (634x59, 3K)

>Here's the problem but don't solve it this way
That's why college is a meme.

(defun digits (int &optional (base 10) (acc ()))
(if (< int base) (
(lambda () (cons int acc))
) (
(lambda () (digits (floor int base) base (cons (rem int base) acc)))
))
)


Havent run that but it should work

what the fuck is LFE? Some sort of erlang-lisp?

this looks more like standard FAANG question shit than college.

#include
#include
#include
#include

#define DIGIT() { if (input) { digits[digitCount++] = input % base; input /= base; } else { break; } }
#define PRINT() { printf("%" PRIu8, digits[--digitCount]); }

const size_t MAX_DIGITS = 32;

int main()
{
uint32_t input, base;
scanf("%" SCNu32 " %" SCNu32, &input, &base);

uint8_t digits[MAX_DIGITS];
memset(digits, 0, MAX_DIGITS);

uint64_t digitCount = 0;
switch (MAX_DIGITS)
{
case 32: DIGIT(); case 31: DIGIT(); case 30: DIGIT(); case 29: DIGIT(); case 28: DIGIT(); case 27: DIGIT(); case 26: DIGIT(); case 25: DIGIT();
case 24: DIGIT(); case 23: DIGIT(); case 22: DIGIT(); case 21: DIGIT(); case 20: DIGIT(); case 19: DIGIT(); case 18: DIGIT(); case 17: DIGIT();
case 16: DIGIT(); case 15: DIGIT(); case 14: DIGIT(); case 13: DIGIT(); case 12: DIGIT(); case 11: DIGIT(); case 10: DIGIT(); case 9: DIGIT();
case 8: DIGIT(); case 7: DIGIT(); case 6: DIGIT(); case 5: DIGIT(); case 4: DIGIT(); case 3: DIGIT(); case 2: DIGIT(); case 1: DIGIT();
default: break;
}

switch (digitCount)
{
case 32: PRINT(); case 31: PRINT(); case 30: PRINT(); case 29: PRINT(); case 28: PRINT(); case 27: PRINT(); case 26: PRINT(); case 25: PRINT();
case 24: PRINT(); case 23: PRINT(); case 22: PRINT(); case 21: PRINT(); case 20: PRINT(); case 19: PRINT(); case 18: PRINT(); case 17: PRINT();
case 16: PRINT(); case 15: PRINT(); case 14: PRINT(); case 13: PRINT(); case 12: PRINT(); case 11: PRINT(); case 10: PRINT(); case 9: PRINT();
case 8: PRINT(); case 7: PRINT(); case 6: PRINT(); case 5: PRINT(); case 4: PRINT(); case 3: PRINT(); case 2: PRINT(); case 1: PRINT();
default: printf("\n"); break;
}

return 0;
}

from sys import argv
ans = argv[1]

# strings work the same as lists in python
# command line arguments are all strings
# the only problem with this is the numbers
# in the string are chars instead of ints now

explode base = rec [] where
rec acc x
| x < base = x : acc
| otherwise = rec (mod x base : acc) (div x base)

welcome to Jow Forums

The dude probably tried to solve the problem, failed after like 2 minutes, and instead of trying to push through it, decided to be an autistic sperglord and nitpick it.
It stems from insecurity, if he knew how to do it he would have just posted the answer. Its why these losers never wind up getting jobs at real tech companies and so move into shit like IT, where they just provision laptops all day and then come home and whine on Jow Forums and Jow Forums

I've seen the occasional one of these dickheads make it through to an acutal onsite before, they would take a stab at the question, get it wrong, and then just start nit-picking the question.

Protip to any retards who get angry reading this: don't criticize your interviewer to their face, no matter how retarded the question. You can pick apart the question and ask for clarification, but dont be a retard about it.