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
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
Other urls found in this thread:
google.com
twitter.com
>takes 3 integers
>not an arbitrary amount of integers
Step it up fampai.
>fn(9, 100, 2) == 10092
> 10092 > 92100
Nice test case.
>fn(9, 100, 2) == 10092
>largest combination possible
you fucking wot m8
fn (9,100,2) should be 92100 though
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?
the first if is useless
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?
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.
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))))))
cute string hehe
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?
I'm stealing a post for your intentional error to get more bumps.
t. wise man
>communist op
>can't even count
pottery
Too lazy to type so have this:
some array with input
Sort input high to low
Mash together and printf
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.
// 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);
Nope, 50 is more than 9, gotta sort so 9 is first, not high to low
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;
Oh you don't know the half of it, user.
I feel like I have transcended Sup Forums a long time ago.
all you have to do is read them into an array, sort the array then unpack them, its trivial as fuck.
All the people in this thread misunderstanding the prompt
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
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)
])
Oh nvm just read OP again and he has it the first way i said it, not even as hard
Are you saying I misunderstood it?
Op was wrong. Also, string comparison uses the ASCII values, not the integer the string represents. So, yes it's very simple.
>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)
may*
there are*
My keyboard is dying
0/10
0/10
0/10
0/10
wtf dude 0/10
0/10
10/10
print max
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.
>accepts negative numbers
>doesn't even check if they're numbers
>not a language
>i can't read it, so you get a pass
>valid pajeet
>pass
>valid freshman
>accepts non-integers
step it up
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;
}
You wouldn't kill a commie loli
Maybe don't give it non-integer arguments, famalam.
>Necrophilia
You're sick.
Who are you quoting?
Myself.
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) + '
');
document.write(JSON.stringify(list2) + '
');
compareMain(list1);
compareMain(list2);
document.write('
');
document.write(JSON.stringify(list1) + '
');
document.write(JSON.stringify(list2) + '
');
}
runIt();
How do you guarantee that in Ruby?
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 "
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]))
>not a language
>implying that matters
const fn = (...numbers) => numbers.sort((a, b) => b - a).reduce((p, c) => p + c, '')
And you say JavaScript is ugly?
>Ada
I am so fucking brilliant, took me like 1 minute
Funny thing, this is productioncode.
>230, 23
>Max is 23023
Nailed it
btfo, how will he ever recover?
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.
>largest combination
Do you mean "in order from largest to smallest"? Because that's not the largest combination from 9, 100, 2.
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.
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.
This brought it to my attention. They simply redefined space as bigger than the numbers.
Neat
He is a tripfag who is fed by attention.
By YOUR attention.
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
Yours too, user.
>ruby
>sempai
>WA
are you ian?
pretty fucking trivial lol
Who's Ian?
my senpai in wa who loves ruby
And who are you?
someone who hates js but loves scheme
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
Congratulations, you're hired!
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)])