Napisz ten sam program w innym języku od poprzedniego

0

Znajdź najmniejszą i największą liczbę! Ruby 3.2 YJIT WebAssembly. Warto też dopisać wersję języka i kompilatora/interpretera w którym dany program został skompilowany i uruchomiony.

# create a method that finds the minimum value passed into it
def find_min(*args) # allows me to pass in any amount of arguments
  min_num = args[0]
  args.each do |number|
    if number < min_num
      min_num = number
    end
  end
  min_num
end

puts(find_min(1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5))

# create a method that finds the maximum value passed into it
def find_max(*args) # allows me to pass in any amount of arguments
  max_num = args[0]
  args.each do |number|
    if number > max_num
      max_num = number
    end
  end
  max_num
end

puts(find_max(1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5))

0

Chat-GPT zamknęli?
A nie czekaj, działa dalej - no to:

// W języku Go program wyglądałby tak:
sdsd
package main

import (
	"fmt"
)

func findMin(numbers ...int) int {
	minNum := numbers[0]
	for _, number := range numbers {
		if number < minNum {
			minNum = number
		}
	}
	return minNum
}

func findMax(numbers ...int) int {
	maxNum := numbers[0]
	for _, number := range numbers {
		if number > maxNum {
			maxNum = number
		}
	}
	return maxNum
}

func main() {
	fmt.Println(findMin(1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5))
	fmt.Println(findMax(1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5))
}
4
#include <algorithm>
#include <iostream>

int main() {
    auto [min, max] = std::minmax({1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5});
    std::cout << min << ", " << max << "\n";
}
0

JS:

const a = [1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5]
console.log("min =", a.reduce((a,b)=>Math.min(a, b)));
console.log("max =", a.reduce((a,b)=>Math.max(a, b)));
0

Java 19.0.1 OpenJDK

public class Main
{
	public static void main(String[] args) {
		
        int [] arr = new int [] {1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5};  
 
        int min = arr[0]; 
        int max = arr[0]; 
 
        for (int i = 0; i < arr.length; i++) {  
 
            if(arr[i] < min)  
               min = arr[i]; 
 
            if(arr[i] > max)  
               max = arr[i]; 
        }
 
        System.out.println("Min element in the array: " + min);
 
        System.out.println("Max element in the array: " + max);
    }  
	
}
0

@sheepard: skompilowane do targetu 1.1 - działa :-P

screenshot-20230201184814.png

1
sheepard napisał(a):

Znajdź najmniejszą i największą liczbę! Ruby 3.2 YJIT WebAssembly. Warto też dopisać wersję języka i kompilatora/interpretera w którym dany program został skompilowany i uruchomiony.
[...]

Clojure, 1.8.0

(ns org.Riddle
	(:gen-class))

(def array [10, 2, 3, 1, 20])
(println
    (apply max array))
(println
    (apply min array))

Za łatwy program wybrałeś, bo większość języków ma metody min i max w bibliotece standardowej.

0

Python 3.11

l = [1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5]
print(f'Min: {min(l)}, Max: {max(l)}')
2
.include dos2.inc
.include 8051.s
.include putn.s

main:   mov     dptr, #nums
        mov     r6, #0
        mov     r7, #255
loop:   movx    a, @dptr
        inc     dptr
        cjne    a, #255, gnum
        mov     r5, 6
        acall   pnum
        mov     r5, 7
        acall   pnum
        ljmp    dos_exit
gnum:   xrl     a, #128
        cjne    a, 7, $+3
        jnc     $+3
        mov     r7, a
        cjne    a, 6, $+3
        jc      $+3
        mov     r6, a
        sjmp    loop

pnum:   mov     a, r5
        jnb     acc.7, pneg
        mov     r0, #43
        lcall   dos_putch
        mov     r0, 5
        xrl     0, #128
pnumo:  acall   putn8
        mov     r0, #10
        lcall   dos_putch
        ret
pneg:   mov     r0, #45
        lcall   dos_putch
        mov     r0, 5
        xrl     0, #127
        inc     r0
        sjmp    pnumo

nums:
        db      1, 5, 0-3, 6, 8, 0, 0-10, 121, 3, 4, 4, 5
        db      255
2

SCSS

@use "sass:list";

@function find-min-and-max($values) {
  $min-value: list.nth($values, 1);
  $max-value: list.nth($values, 1);
  
  @each $value in $values {
    @if $min-value > $value {
      $min-value: $value;
    }
    
    @if $max-value < $value {
      $max-value: $value;
    }
  }
  
  @return ($min-value, $max-value);
}

@debug find-min-and-max((1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 5));
2
select min(x), max(x) from unnest(array[1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5]) x(x)

postgreSQL

0
a: min 1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5
b: max 1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5

Q

0
        Dim l As New List(Of Integer)({1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 5})
        Console.WriteLine("Max: {0} Min: {1}", l.Max, l.Min)

VB.NET

0

Dart prawie jak JS:

import 'dart:math';

void main() {
final a = [1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5];
print('min = ${a.reduce(min)}, max = ${a.reduce(max)}');
}

(nie ma tu kolorowania składni dart)

2

05AB1E:

W,Z,

try-it online

4

screenshot-20230202145946.png
screenshot-20230202145951.png

2
IDENTIFICATION DIVISION.
PROGRAM-ID. MIN-MAX.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        77 NUMS PIC S9(3) OCCURS 12 TIMES INDEXED BY I.
        77 MIN PIC S9(3).
        77 MAX PIC S9(3).
PROCEDURE DIVISION.
    MOVE   1 TO NUMS(1)
    MOVE   5 TO NUMS(2)
    MOVE  -3 TO NUMS(3)
    MOVE   6 TO NUMS(4)
    MOVE   8 TO NUMS(5)
    MOVE   0 TO NUMS(6)
    MOVE -10 TO NUMS(7)
    MOVE 121 TO NUMS(8)
    MOVE   3 TO NUMS(9)
    MOVE   4 TO NUMS(10)
    MOVE   4 TO NUMS(11)
    MOVE   5 TO NUMS(12)
    
    SET MIN TO +999.
    SET MAX TO -999.
    PERFORM VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN 12
        IF MIN GREATER THAN NUMS(I) THEN SET MIN TO NUMS(I) END-IF
        IF MAX IS LESS THAN NUMS(I) THEN SET MAX TO NUMS(I) END-IF
    END-PERFORM.
    DISPLAY "MIN = "MIN.
    DISPLAY "MAX = "MAX.
STOP RUN.
0

c#

int[] arr = { 1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5 };
Console.WriteLine($"Min: {arr.Min()}, Max: {arr.Max()}");

był lata temu już taki wątek, ktoś ma linka?

był kiedyś podobny wątek: konkursik [1500 downto 0]

1

CSS:

.max {
  margin-left: max(1px, 5px, -3px, 6px, 8px, 0px, -10px, 121px, 3px, 4px, 4px, 5px);
}

.min {
  margin-left: min(1px, 5px, -3px, 6px, 8px, 0px, -10px, 121px, 3px, 4px, 4px, 5px);
}

przykład: https://jsfiddle.net/b956up1g/
(wraz z kodem HTML i dodatkową klasą container i ramką, żeby było widać te marginesy)

0
#!/bin/sh

max=-99999
min=99999

for i in 1 5 -3 6 8 0 -10 121 3 4 4 5; do
        test $min -gt $i && min=$i
        test $max -lt $i && max=$i
done

echo "min = $min, max = $max"
1

JSON

{
   "min": -10,
   "max": 121
}
0
        .globl  main

main:   subq    $8, %rsp
        leaq    nums(%rip), %rsi
        lodsl
        movl    %eax, %ecx
        movl    %eax, %edx
loop:   lodsl
        cmpl    $-1U, %eax
        je      fini
        cmpl    %eax, %ecx
        cmovg   %eax, %ecx
        cmpl    %eax, %edx
        cmovl   %eax, %edx
        jmp     loop
fini:   leaq    fmt(%rip), %rdi
        movl    %ecx, %esi
        call    printf
        addq    $8, %rsp
        ret

        .section .rodata
fmt:    .asciz  "min = %i, max = %i\n"
nums:   .long   1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5, -1U
0

Kto napisze w Brainfuck?

0

Co prawda nie jest to BF, ale tego języka jeszcze nie było:

module globals
    implicit none
    
    integer, dimension (12) :: numbers = (/1, 5, -3, 6, 8, 0, -10, 121, 3, 4, 4, 5/)
end module globals

program minmax
    use globals
    implicit none
    
    integer :: min, max
    integer :: i

    min = numbers(1)
    max = numbers(1)

    do i = 1, size(numbers)
        if (numbers(i) > max) then; max = numbers(i); end if
        if (numbers(i) < max) then; min = numbers(i); end if
    end do

    Print *, "min =", min
    Print *, "max =", max
end program minmax

1 użytkowników online, w tym zalogowanych: 0, gości: 1