6 kyu

Reducing by steps

1,203 of 4,522g964

Description:

Data: an array of integers, a function f of two variables and an init value.

Example: a = [2, 4, 6, 8, 10, 20], f(x, y) = x + y; init = 0

Output: an array of integers, say r, such that

r = [r[0] = f(init, a[0]), r[1] = f(r[0], a[1]), r[2] = f(r[1], a[2]), ...]

With our example: r = [2, 6, 12, 20, 30, 50]

Task:

Write the following functions of two variables

  • som : (x, y) -> x + y
  • mini : (x, y) -> min(x, y)
  • maxi : (x, y) -> max(x, y)
  • lcmu : (x, y) -> lcm(abs(x), abs(y) (see note for lcm)
  • gcdi : (x, y) -> gcd(abs(x), abs(y) (see note for gcd)

and

  • function oper_array(fct, arr, init) (or operArray or oper-array) where

  • fct is the function of two variables to apply to the array arr (fct will be one of som, mini, maxi, lcmu or gcdi)

  • init is the initial value

Examples:

a = [18, 69, -90, -78, 65, 40]
oper_array(gcd, a, a[0]) => [18, 3, 3, 3, 1, 1]
oper_array(lcm, a, a[0]) => [18, 414, 2070, 26910, 26910, 107640]
oper_array(sum, a, 0) => [18, 87, -3, -81, -16, 24]
oper_array(min, a, a[0]) => [18, 18, -90, -90, -90, -90]
oper_array(max, a, a[0]) => [18, 69, 69, 69, 69, 69]

Notes:

  • The form of the parameter fct in oper_array (or operArray or oper-array) changes according to the language. You can see each form according to the language in "Your test cases".

  • AFAIK there are no corner cases, everything is as nice as possible.

  • lcm and gcd see: https://en.wikipedia.org/wiki/Least_common_multiple https://en.wikipedia.org/wiki/Greatest_common_divisor

  • you could google "reduce function (your language)" to have a general view about the reduce functions.

  • In Shell bash, arrays are replaced by strings.

  • In OCaml arrays are replaced by lists.

Mathematics
Arrays
Functional Programming
Lists
Data Structures

Similar Kata:

Stats:

CreatedMar 21, 2016
PublishedMar 21, 2016
Warriors Trained19904
Total Skips5087
Total Code Submissions22444
Total Times Completed4522
Ruby Completions115
Python Completions964
JavaScript Completions1203
CoffeeScript Completions17
Haskell Completions174
Clojure Completions39
Java Completions305
C# Completions170
Elixir Completions96
C++ Completions225
PHP Completions99
TypeScript Completions91
Crystal Completions10
F# Completions45
C Completions191
OCaml Completions47
Rust Completions231
Swift Completions122
Go Completions313
R Completions34
Nim Completions19
Shell Completions20
Kotlin Completions94
Julia Completions32
Scala Completions88
Reason Completions5
Racket Completions21
VB Completions23
Prolog Completions16
Pascal Completions7
Lua Completions36
Perl Completions10
Raku Completions7
Elm Completions6
D Completions5
Erlang Completions8
Factor Completions7
Total Stars306
% of votes with a positive feedback rating84% of 675
Total "Very Satisfied" Votes502
Total "Somewhat Satisfied" Votes129
Total "Not Satisfied" Votes44
Ad
Contributors
  • g964 Avatar
  • NaMe613 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • trashy_incel Avatar
  • Kacarott Avatar
  • Just4FunCoder Avatar
  • razetime Avatar
Ad