Function that accepts 2 integers and returns the one nearest 1000. Go

Function that accepts 2 integers and returns the one nearest 1000. Go.

Other urls found in this thread:

repl.it/languages/python3
docs.python.org/3/library/functions.html
twitter.com/SFWRedditImages

Homework threads belong in /wsr/

Compare the difference and return one with lowest dif
And see

public static int closestToThousand (int a, int b) {
return (1000-a>-1?1000-a:1000-a*-1)>(1000-b>-1?1000-b:1000-b*-1)?b:a;
}

lulz

#include

inline int abs(int a) { return a < 0 ? -a : a; }

int NearestTo(const int A, const int B, const int to) {
return (abs(A-to) < abs(B-to) ? A : B);
}

int main(int argc, char** argv) {
printf("%i\n", NearestTo(1000, 1101, 1000));
return 0;
}

#!/usr/bin/env bash
a1="${1}"
b1="${2}"
a2=$((1000-${a1}))
b2=$((1000-${b1}))
if [[ "${a2}" -lt "0" ]]; then
a2=$((${a2} * -1))
fi
if [[ "${b2}" -lt "0" ]]; then
b2=$((${b2} * -1))
fi
if [[ "${a2}" -lt "${b2}" ]]; then
echo "${a1} is nearest 1000 than ${b1}"
elif [[ "${b2}" -lt "${a2}" ]]; then
echo "${b1} is nearest 1000 than ${a1}"
fi

ugliest shit i've ever seen

Don't fucking reply to me unless you're contributing to the thread.

code
public int oneThowznd(int a, int b)
{
a = 1000 - a;
b = 1000 - b;
if(a < b && b < 1000)
return b;
else if(b < a && a < 1000)
return a;
//both are greater than 1000
else if( a < b)
return a;
else if (b < a)
return b;
//they are equal return a
return a;
}
/code

func:
mov rax, rdi
mov rdx, rsi
sub rax, 1000
mov rcx, rax
neg rax
cmovs rax, rcx
sub rdx, 1000
mov rcx, rdx
neg rdx
cmovs rdx, rcx
cmp rax, rdx
cmova rdi, rsi
mov rax, rdi
ret

why? its fun to post your own code and have random people shit on you for coding in Java

Python3
def op_is_a_faggot(int1, int2):
if (abs(1000 - int1) < abs(1000 - int2)):
return int1
else:
return int2

Bash is repulsive.

auto foo(int a, int b) {return "the one nearest to 1000";}

>programmers program in their language
>java people prefer to "code" in theirs

Karly koder detected

>people don't have languages they're most comfortable with

>Karly koder
teach me this meme

google kode with karly or some shit.

OP, what if you pass 1001 or 999?

...

>int1 = 998
>int2 = 999

fails for a == 1002, b == 999. In this case, b should be returned, but your code will return a.

>the one nearest 1000

Read the prompt carefully next time. My code returns 999, which is closer to 1000 than 998.

string nearest_to_1000 (int a, int b)
{
return "the one nearest 1000. Go."
}

But 998 - 1000 will equal -2 which is less than 999 - 1000 (-1) so
>if (abs(1000 - int1) < abs(1000 - int2))
will return true and return int1, 998

Go to repl.it/languages/python3 and run the following:

def op_is_a_faggot(int1, int2):
if (abs(1000 - int1) < abs(1000 - int2)):
return int1
else:
return int2

print(op_is_a_faggot(998, 999))

tell me what you get

Don't you know what abs() does, you fucking retard?

you fuckin mongo what do you think abs() does

holy shit please commit abs(-sudoku)

>these are the kind of people posting on Sup Forums

le powershell
[int]$number1 = Read-Host 'Enter first number'
[int]$number2 = Read-Host 'Enter second number'
$result1 = [math]::abs($number1 - 1000)
$result2 = [math]::abs($number2 - 1000)

If($result1 -gt $result2) {
Write-Host "$number2 is closer to 1000."
}
Elseif ($result1 -eq $result2) {
Write-Host "Both numbers are equal."
}
Else {
Write-Host "$number1 is closer to 1000."
}

Why can't I print the output of abs?

Yeah i am aware i just wanted to post something quick

How do you actually run this in powershell?

int nearest(int a, int b)
{
return 2000 ^^ 2 - a ^^ 2 > 2000 ^^ 2 - b ^^ 2 ? b : a;
}

>AMD64

why can't you really been far even as decided to use even go want to do look more like?

cd
cd
cd
kode
cd
cd
ls
cd
kode

>using prologue and epilogue when you're not using the stack

...

func(int x, int y) {
return abs(1000-x) < abs(1000-y) ? x : y;
}

abs() is one of many functions built in to Python. You can find out more about these functions here:
docs.python.org/3/library/functions.html

Try running print(abs(1))
print(abs(-1))

Open Powershell ISE, paste code, press F5 to run

what abomination of a programming language is this?

this

ngl i was kinda hoping you'd take a picture of your screen using your phone for this one

>not knowing modern asm

>is best

This is why we need to ban IPs from India and similar shitholes.

>>not knowing modern asm
Nothing wrong with KNOWING it, but using 64-bit code seems like overkill given the requirements for this problem, and doesn't seem worth the loss in backwards compatibility.

>three functions
>doesn't return the answer

0/10

nigga it's a shitty bait thread on Sup Forums who cares

clojure
(defn nearest1k [x y]
(if (< (Math/abs (- 1000 x))
(Math/abs (- 1000 y)))
x y))

#define near1k(a, b) (abs(a - 1000) < abs(b - 1000)? a: b)

Paul Graham just inexplicably got an erection

who on earth keeps making these lisp stealies?
and why do they care about anything that isn't guile?

opIsAnEnormousFaggot :: Integer -> Integer -> Integer
opIsAnEnormousFaggot x y
| x==y = error "Equality was a misake"
| abs(1000-x)

>error "Equality was a misake"

ammmmmmmmmmm

are you kidding me you fucking retard?

>not using 1st class fns to generalize the problem first
what are you even LISP-ing for?

(defn nearest-value [target x y]
(if (< (Math/abs (- target x))
(Math/abs (- target y)))
x y))

(def nearest1k
(partial nearest-value 1000))

lambda a,b: min((a,b),key=lambda x: abs(1000-x))

>What's wrong user? Don't you know that Donald Trump is bad for America? That's even more obvious than a solution to the minimum-coin-change problem given in Reverse-Polish Notation!

support for longs is better than support for 32 bit chinkpads

64 bit code with no multiplcation or division is just as fast as 32 bit code.

; bc - num 1
; de - num 2
func:
push bc
push de
ld hl, 1000
ccf
sbc hl, de
ex de, hl
ld hl, 1000
ccf
sbc hl, bc
or a
sbc hl, de
add hl, de
pop hl
jr c, _
pop hl
ret
_:
pop de
ret


been a while so its probably wrong

Yea this is basically what I did when I read OP

if |number1 - 1000| > |number2 - 100|

I was waiting for someone to do this with a lambda. Nice one.

Why not Go?
package main

import (
"fmt"
"math"
)

func derp(n1, n2 float64) float64 {
if math.Abs(1000-n1) < math.Abs(1000-n2) {
return n1
} else {
return n2
}
}

func main() {
var n1 float64
fmt.Scanf("%f", &n1)
var n2 float64
fmt.Scanf("%f", &n2)
fmt.Println(derp(n1, n2))
}

nice didn't know we had google employees on Sup Forums these days

>not doing it in lambda calculus
Lightweight baby.

def nearestTo1k(a,b):
if a > b:
if a

>100

DEF FNs(a%,b%)
LOCAL result%
IF ABS(a%-1000) < ABS(b%-1000) THEN result%=a% ELSE result%=b%
=result%

puts "please enter a number!"
a = gets.chomp
puts "please enter another number!"
b = gets.chomp

if a > b
puts a
else
puts b
end

puts "please kill yourself now!"

That was absolutely terrible.

a = 1001
b = 1002

you're a retard

okay

Am I contributing?

(defn nearest [x y] (min-key #(Math/abs (- % 1000)) x y))

min([a, b], key=lambda o: abs(1000 - o))
Also extends to any number of numbers.

clojure is the comfiest language.

Kill yourself, you retarded idiot.

abs(integer - 1000)

you got it bro

Sure thing Jimmy

int closestToThousand(int a, int b) {
int absA;
int absB;

absA = abs(1000 - a);
absB = abs(1000 - b);

if(absA

>-3 is closer to 1000 than 5

jfc that is repugnant

unacceptable

Here. Have some java. Everything you've heard about java's verbosity is true.

import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class OPHomework {

@Test
public void doOPHomework() {
int target = 1000;
int a = 997;
int b = 1017;

Goal getNearest = new Goal(Goal.NEAREST);

int closest = getNearest.to(target).of(a,b);

assertThat(closest,is(a));

}

private class Goal {
public static final String NEAREST = "nearest";

private String goal;

public Goal(String goal) {
this.goal = goal;
}

public Of to(int target) {
switch(goal) {
case NEAREST:
default:
return new OfNearestIntImpl(target);
}
}


}

private class OfNearestIntImpl implements Of {
private int target;

public OfNearestIntImpl(int target) {
this.target = target;
}

@Override
public Integer of(Integer a, Integer b) {
return Math.abs(target - a) < Math.abs(target -b) ? a : b;
}
}

private interface Of {
public T of(T a, T b);
}
}

int closestToThousand(int a, int b) {
int absA;
int absB;

absA = abs(1000 - abs(a));
absB = abs(1000 - abs(b));

if(absA

>-3 is closer to 1000 than 5

Have the python form of the ternary operator
def closest1000(a, b):
return (b,a)[abs(1000 - a < 1000 - b)]

> -3 is closer to 1000 than 5

I missed a bracket

Test it. True indexed as 1, so the list order is reversed.

>-3 is closer to 1000 than 5

>asked for a function
>produces a shitheap of a program
>asked for input
>shitheap program doesn't ask for or accept any input
Wow... so this... is the power of java

>-3 is closer to 1000 than 5

>>> def closest1000(a, b):
... return (b,a)[abs(1000 - a < 1000 - b)]
...
>>> closest1000(-3,100)
100

>-3 is closer to 1000 than 5

You're fucking retarded