[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Write a program that takes 3 integers from the users and prints

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 71
Thread images: 6

File: 1467581451205.jpg (88KB, 870x960px) Image search: [Google]
1467581451205.jpg
88KB, 870x960px
Write a program that takes 3 integers from the users and prints the largest combination possible.

so
fn(9, 100, 2) == 10092
fn(21,77,1) == 77211
>>
>>60007883
>takes 3 integers
>not an arbitrary amount of integers
Step it up fampai.
>>
>>60007883
>fn(9, 100, 2) == 10092
> 10092 > 92100
Nice test case.
>>
>fn(9, 100, 2) == 10092
>largest combination possible
you fucking wot m8
>>
>>60007883
fn (9,100,2) should be 92100 though
>>
File: akari :(.jpg (308KB, 712x1134px) Image search: [Google]
akari :(.jpg
308KB, 712x1134px
>>60007883
What a poorly defined problem.
You mean the largest number obtainable from pasting the 3 numbers together?
Your example isn't even correct.
>>
int fn(int a, int b, int c)
{
char buf[20] = { 0 };
int lg = 0;
if (sprintf(buf, "%d%d%d", a, b, c) && atoi(buf) > lg)
lg = atoi(buf);
if (sprintf(buf, "%d%d%d", a, c, b) && atoi(buf) > lg)
lg = atoi(buf);
if (sprintf(buf, "%d%d%d", b, a, c) && atoi(buf) > lg)
lg = atoi(buf);
if (sprintf(buf, "%d%d%d", c, a, b) && atoi(buf) > lg)
lg = atoi(buf);
if (sprintf(buf, "%d%d%d", b, c, a) && atoi(buf) > lg)
lg = atoi(buf);
if (sprintf(buf, "%d%d%d", c, b, a) && atoi(buf) > lg)
lg = atoi(buf);
return lg;
}


Can I have job now?
>>
>>60008065
the first if is useless
>>
>>60007883
def fn(*args):
nums = [str(i) for i in list(args)]
nums.sort()
nums.reverse()
return int(''.join(nums))
>>
So, a program that sorts integers in descending order?
>>
>>60007883
Pseudocode

int numbers = [9, 100, 2]
int Max = 0

Int i = 0
while ++i < 20 {
Array.shuffle(numbers)
If test = int(numbers.concat()) > max {
max = test
}
}

print test


You can shuffle the array more times to make sure the max number is the largest possible combination.
>>
>>60009243
sort by first digit. Basically a tokenizing problem without the tokenizing.
>>
(import (srfi srfi-26))

(define (fn . args)
(define (on f g) (lambda (. x) (apply f (map g x))))
(string->number (string-concatenate (sort (map number->string args)
(on char>? (cute string-ref <> 0))))))
>>
>>60009442
cute string hehe
>>
>>60007883
Bump. Also idiot.
>>
help me programmer anons, I want to learn basic coding this summer. I'm an econ major and know nothing about this. Is Python a good language to learn? are there any top notch resources available?
>>
>>60007883
I'm stealing a post for your intentional error to get more bumps.
t. wise man
>>
>>60007883
>communist op
>can't even count
pottery
>>
>>60007883
Too lazy to type so have this:
 some array with input
Sort input high to low
Mash together and printf
>>
>>60007883
Cute Kyouko.
>>
Sort the input set of integers by looking comparing the digits of the integers, break ties in the nth digit by comparing n+1st digits. If one of the integers has no n+1st digit, it's considered larger. Reverse and concatenate.

OP's homework done, just implement the algorithm.
>>
File: lisa-needs-braces.jpg (24KB, 395x466px) Image search: [Google]
lisa-needs-braces.jpg
24KB, 395x466px
// Javascript
function biggest_number_on_number_join_as_string(a, b, c) {
var biggest_number = 0;
var test_case = '' + a + b + c;
if (biggest_number < test_case) {
biggest_number = test_case;
}
test_case = '' + a + c + b;
if (biggest_number < test_case) {
biggest_number = test_case;
}
test_case = '' + b + a + c;
if (biggest_number < test_case) {
biggest_number = test_case;
}
test_case = '' + b + c + a;
if (biggest_number < test_case) {
biggest_number = test_case;
}
test_case = '' + c + a + b;
if (biggest_number < test_case) {
biggest_number = test_case;
}
test_case = '' + c + b + a;
if (biggest_number < test_case) {
biggest_number = test_case;
}
return biggest_number
}

var result = biggest_number_on_number_join_as_string(9, 80, 4);
console.log(result);
>>
>>60011572
Nope, 50 is more than 9, gotta sort so 9 is first, not high to low
>>
>>60007883
Generic sized, type verified Ada kino coming through
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Containers.Generic_Constrained_Array_Sort;

procedure Biggest_Concat is

subtype Small_String is String (1 .. 10) with
Dynamic_Predicate => (for all I of Small_String => (I in '0' .. '9' | ' '));
subtype Index_Range is Positive range 1 .. Argument_Count;
type Input_Array_T is array (Index_Range) of Small_String;

Input : Input_Array_T;

procedure Sort is new Ada.Containers.Generic_Constrained_Array_Sort
(Index_Range, Small_String, Input_Array_T);

begin -- Biggest_Concat

for I in Input'Range loop
Move(Argument(I), Input(I));
if Input(I) not in Small_String then
raise Constraint_Error with "input not a number";
end if;
end loop;

Sort(Input);

for I of reverse Input loop
Put(Trim(I, Both));
end loop;

end Biggest_Concat;
>>
>>60011546
Oh you don't know the half of it, anon.
I feel like I have transcended 4chan a long time ago.
>>
>>60007883
all you have to do is read them into an array, sort the array then unpack them, its trivial as fuck.
>>
>>60011883
All the people in this thread misunderstanding the prompt
>>
>>60011883
sort them based on the integer in the 0 position that is, forgot to add that, since if you have 9 and 10 910 is bigger than 109 even though 9 is smaller than 10
>>
File: me.png (75KB, 430x441px) Image search: [Google]
me.png
75KB, 430x441px
def comb3(a, b, c):
a = str(a)
b = str(b)
c = str(c)

return max([
int(a + b + c),
int(a + c + b),
int(b + a + c),
int(b + c + a),
int(c + a + b),
int(c + b + a)
])
>>
>>60011916
Oh nvm just read OP again and he has it the first way i said it, not even as hard

>>60011915
Are you saying I misunderstood it?
>>
>>60011932
Op was wrong. Also, string comparison uses the ASCII values, not the integer the string represents. So, yes it's very simple.
>>
>>60009257
>first digit sorting
Not as easy as you ma think, if there two first same digits, you have compare the second one onwards
12, 8,(900,912,97)
>>
>>60011949
may*
there are*
My keyboard is dying
>>
>>60008065
0/10
>>60009094
0/10
>>60009244
0/10
>>60011572
0/10
>>60011742
wtf dude 0/10
>>60011883
0/10
>>60011922
10/10
>>
>>60009244
print max
>>
>>60007883
We should kill all commies we can
>>
def fn *args
args.map(&:to_s).sort { |x,y| y <=> x }.join('').display
end


Since we use string comparison, we guarantee that for any two integers, those with the highest digits in the highest places always end up first. When given a choice between, say, 86 and 842, we place 86 first since it is higher in the second position. Were we to have placed it the other way around, we would necessarily have had a lower number regardless of the rest of the digits, since there would be a lower number in the second highest spot.
>>
>>60008065
>accepts negative numbers
>>60009094
>doesn't even check if they're numbers
>>60009244
>not a language
>>60009442
>i can't read it, so you get a pass
>>60011742
>valid pajeet
>>60011835
>pass
>>60011922
>valid freshman
>>60012263
>accepts non-integers
step it up
>>
File: 1483252631529.png (47KB, 785x924px) Image search: [Google]
1483252631529.png
47KB, 785x924px
import std.stdio;
import std.conv;

void main(string[] args)
in//sanitize input: break by spaces
{
for (int i = 1; i < args.length; i++) //for each numbers
{
for (int j = 0; j < args[i].length; j++) //for each digits
{
char x = args[i][j];
assert(((x > 47 && x < 58) || (x == 32)), "Invalid input");
}
}
}
body
{
string[] numbers = args[1 .. $];
//sort by first digits
sort_by_first_digits(numbers);
//convert to a number
auto result = make_number(numbers);
//print number
writeln(result);
}

void sort_by_first_digits(ref string[] result)
{
//I see what you did there, OP.
//TODO: Recursively compare the first digits
}

ulong make_number(string[] items)
{
ulong result;
string concatenated_number;
for (int i = 0; i < items.length; i++)
{
concatenated_number ~= items[i];
}
result = to!ulong(concatenated_number);
return result;
}

>>60012144
You wouldn't kill a commie loli
>>
>>60012323

Maybe don't give it non-integer arguments, famalam.
>>
>>60012457
>Necrophilia
You're sick.
>>
>>60012516
Myself.
>>
<!DOCTYPE html>
<html>
<body>

<script>
function compare(a, b){

var a1 = a.toString();
var b1 = b.toString();
var a2, b2;

if(a1.length > b1.length){
a1 = a1.substring(0, b1.length);
a2 = parseInt(a1);
b2 = parseInt(b1);

return a2 - b2;
}
else if(a1.length < b1.length){
b1 = b1.substring(0, a1.length);
a2 = parseInt(a1);
b2 = parseInt(b1);

return a2- b2
}
else {
return a - b;
}
return 0;
}

function compareMain(list){

var m;

for(var i = 0; i < list.length; i++)
for(var k = 0; k < list.length; k++){
if(compare(list[i], list[k]) > 0){
m = list[i];
list[i] = list[k];
list[k] = m;
}
}
}

function runIt() {

var list1 = [9, 100, 2];
var list2 = [21,77, 1];

document.write(JSON.stringify(list1) + '<br>');
document.write(JSON.stringify(list2) + '<br>');
compareMain(list1);
compareMain(list2);
document.write('<br>');
document.write(JSON.stringify(list1) + '<br>');
document.write(JSON.stringify(list2) + '<br>');
}

runIt();
</script>

</body>
</html>
>>
>>60012476
How do you guarantee that in Ruby?
>>
>>60011835
I just realized that the of the posts that do it the smarter way have a fringe case that they fail. Here is the revised version.

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Containers.Generic_Constrained_Array_Sort;

procedure Biggest_Concat is

subtype Small_String is String (1 .. 10);

subtype Number_String is String with
Dynamic_Predicate => (for all I of Number_String => (I in '0' .. '9'));

subtype Index_Range is Positive range 1 .. Argument_Count;

type Input_Array_T is array (Index_Range) of Small_String;

Input : Input_Array_T;


function "<" (Left, Right : in Small_String) return Boolean is
function "<" (Left, Right : in Character) return Boolean is
begin
if Left = ' ' and then Right /= ' ' then
return False;
elsif Left /= ' ' and then Right = ' ' then
return True;
else
return Character'Pos(Left) < Character'Pos(Right);
end if;
end "<";
begin
return (for all I in Small_String'Range =>
Left(I) < Right(I));
end "<";

procedure Sort is new Ada.Containers.Generic_Constrained_Array_Sort
(Index_Range, Small_String, Input_Array_T);

begin -- Biggest_Concat

for I in Input'Range loop
Move(Argument(I), Input(I));
if Trim(Input(I), Right) not in Number_String then
raise Constraint_Error with "input not a number";
end if;
end loop;

Sort(Input);

for I of reverse Input loop
Put(Trim(I, Right));
end loop;

end Biggest_Concat;
>>
>>60007883
def fn(a, b, c):
d = sorted([a, b, c], key=lambda x: x/(10**(len(str(x)))), reverse=True)
print(str(d[0])+str(d[1])+str(d[2]))
>>
>>60012323
>not a language
>implying that matters
>>
https://www.google.com/search?q=program+that+takes+3+integers+from+the+users+and+prints+the+largest+combination+possible

t. Pajeet Kumar
>>
const fn = (...numbers) => numbers.sort((a, b) => b - a).reduce((p, c) => p + c, '')


And you say JavaScript is ugly?
>>
File: 1489262793818.jpg (239KB, 1200x1049px) Image search: [Google]
1489262793818.jpg
239KB, 1200x1049px
>>60011835
>>60012761
>Ada
>>
>>60007883
I am so fucking brilliant, took me like 1 minute
<?php
function fn($a,$b,$c)
{
global $result;
checkacheck("$a$b$c");
checkacheck("$a$c$b");
checkacheck("$a$c$b");
checkacheck("$b$a$c");
checkacheck("$b$c$a");
checkacheck("$c$a$b");
checkacheck("$c$b$a");
return $result;
}
function checkacheck($stack)
{global $result;if ($stack>$result){$result=$stack;}}

echo fn(9, 100, 2);
//92100
?>

Funny thing, this is productioncode.
>>
>>60012263
>230, 23
>Max is 23023
Nailed it
>>
>>60012970
btfo, how will he ever recover?
>>
>>60012726

I mean, you can make it type error if you give it the wrong arguments, but you can't force any sort of static type checking because there is no such thing as compile time in Ruby.

That said, what I'm saying is that it's the user's responsibility not to give it stupid arguments.
>>
>>60007883
>largest combination
Do you mean "in order from largest to smallest"? Because that's not the largest combination from 9, 100, 2.
>>
>>60012970

Ack, you are right. Would need a slightly modified comparison function.

I think the best strategy would probably be to extend out the shorter string to repeat itself in the comparison function, and then truncate that repeated version so that the strings are the exact same size. So 23 temporarily becomes 232, and is compared against 230.
>>
>>60012761
Nice, I was going to do mine in Ada. Have a Lua instead.

-- compare two strings of digits 
-- true if first has a larger digit earlier than the second
-- in other words, true if a..b > b..a (.. is concatenation and > compares numerically)
function cmp(a, b)
if a:len() == b:len() then
return a > b
elseif a:len() < b:len() then
return not cmp(b, a)
elseif a:sub(1,b:len()) == b then
return cmp(a:sub(b:len() + 1), b)
else
return cmp(a:sub(1,b:len()), b)
end
end

-- given any number of natural integer arguments,
-- return the largest integer possible by concatenation of the args
function max_combo(...)
args = table.pack(...)
for k, v in ipairs(args) do
if not tostring(v) then
print(v .. " is not an integer")
return nil
elseif v < 0 then
print(v .. " is less than 0")
return nil
else
args[k] = tostring(v)
end
end

table.sort(args, cmp)

s = ""
for k, v in ipairs(args) do
s = s .. v
end

return s
end

print(max_combo(9, 100, 2))
print(max_combo(21, 77, 1))
print(max_combo(230, 23))


prints
92100
77211
23230


I can explain my logic in the comperator if needed.
>>
>>60013106
This >>60012761 brought it to my attention. They simply redefined space as bigger than the numbers.
>>
>>60012825
Neat
>>
>>60013008
He is a tripfag who is fed by attention.
By YOUR attention.
>>
>>60013131
errored out on negative numbers because I'm pretty sure there's no way to handle them that makes sense for the problem

also I'm realizing that the input validation is incomplete, need to check for non-integer number arguments too.
>>
My revised version:

def fn *args
# For anons who wish to be a pain in the ass about the type of input accepted.
raise TypeError unless args.all? { |x| x.is_a?(Integer) && x >= 0 }
# Lexical sort strings with forced equal size length comparison.
args.map(&:to_s).sort { |x,y|
case x.size <=> y.size
when -1 then y <=> (x * y.size)[0..(y.size - 1)]
when 0 then y <=> x
when 1 then (y * x.size)[0..(x.size - 1)] <=> x
end
}.join('').display
end


>>60013169

Yours too, ANon.
>>
>>60013185
>ruby
>sempai
>WA
are you ian?
>>
>>60007883
pretty fucking trivial lol
>>
>>60013193

Who's Ian?
>>
>>60013219
my senpai in wa who loves ruby
>>
>>60013226

And who are you?
>>
>>60013271
someone who hates js but loves scheme

>>60013131
revised, should sanatize input better but there's probably a better way to do that sanitization.

-- compare two strings of digits 
-- true if first has a larger digit earlier than the second
-- in other words, true if a..b > b..a (.. is concatenation and > compares numerically)
function cmp(a, b)
if a:len() == b:len() then
return a > b
elseif a:len() < b:len() then
return not cmp(b, a)
elseif a:sub(1,b:len()) == b then
return cmp(a:sub(b:len() + 1), b)
else
return cmp(a:sub(1,b:len()), b)
end
end

-- given any number of natural integer arguments,
-- return the largest integer possible by concatenation of the args
function max_combo(...)
args = table.pack(...)
for k, v in ipairs(args) do
assert(tonumber(v) and v >= 0 and math.floor(v) == v, v .. " needs to be a natual integer")
args[k] = tostring(v)
end

table.sort(args, cmp)

s = ""
for k, v in ipairs(args) do
s = s .. v
end

return tonumber(s)
end

-- if we have command line args, use those, otherwise use samples from thread
if arg[1] ~= nil then
for k, v in ipairs(arg) do
arg[k] = tonumber(v)
end
print(max_combo(unpack(arg)))
else
print(max_combo(9, 100, 2))
print(max_combo(21, 77, 1))
print(max_combo(230, 23))
end
>>
I UNDERSTAND NOTHING IN THIS THREAD
>>
>>60013330
Congratulations, you're hired!
>>
>>60011922

def max_comb(arr):
def permute(arr):
def perm(a,k=0):
n = []
if(k==len(a)):
return a
else:
for i in range(k,len(a)):
a[k], a[i] = a[i], a[k]
n += perm(a, k+1)
a[k],a[i] = a[i],a[k]
return n
def segment(a, s):
lenAdS = int(len(a)/s)
return [a[s*(ii-1):s*ii] for ii in range(1, lenAdS+1)]
return segment(perm(arr), len(arr))

arr = [str(ar) for ar in arr]
return max([int( "".join(ar) ) for ar in permute(arr)])
Thread posts: 71
Thread images: 6


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.