Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modulo #14078

Open
its-lee opened this issue Aug 5, 2022 · 3 comments · May be fixed by #14079
Open

Modulo #14078

its-lee opened this issue Aug 5, 2022 · 3 comments · May be fixed by #14079
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@its-lee
Copy link
Contributor

its-lee commented Aug 5, 2022

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Detailed solution/guide is not required, but please be sure the challenge is solvable.

Info

Basic info of your challenge questions,

difficulty: medium
title: Modulo
tags: array, math, infer

Question

Javascript and a lot of other programming languages support the modulo (also know as remainder) operator % which returns the remainder when an integer is divided by another integer. For Javascript's implementation - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder.

Here's some examples:

5 % 4 === 1     // Since 5 divided by 4 is 1 with a remainder of 1
9 % 4 === 1     // Since 9 divided by 4 is 2 with a remainder of 1
16 % 2 === 0    // Since 16 divided by 4 is 8 with a remainder of 0

You do not need to worry about the case when the left or right hand sides are negative.

Template

This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation any.

type Modulo<M extends number, N extends number> = any

Test Cases

Provide some test cases for your challenge, you can use some utils from @type-challenges/utils for asserting.

import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Modulo<5, 4>, 1>>,
  Expect<Equal<Modulo<9, 4>, 1>>,
  Expect<Equal<Modulo<16, 2>, 0>>,
  Expect<Equal<Modulo<35, 1>, 0>>,
  Expect<Equal<Modulo<35, 10>, 5>>
]
@its-lee its-lee added the new-challenge Propose a new challenge, a PR will be auto generated label Aug 5, 2022
@github-actions github-actions bot linked a pull request Aug 5, 2022 that will close this issue
@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2022

#14079 - Pull Request updated.

2022-08-05T15:28:50.362Z Preview in Playground

github-actions bot pushed a commit that referenced this issue Aug 5, 2022
@its-lee
Copy link
Contributor Author

its-lee commented Aug 5, 2022

Here's one way to do it:

type ToArray<N extends number, _Result extends number[] = []> =
  _Result extends { length: N }
  ? _Result
  : ToArray<N, [..._Result, any]>;

type Subtract<A extends any[], B extends any[]> =
  A extends [...infer C, ...B]
  ? C
  : never;

type ModulusOfArray<A extends any[], N extends any[]> =
  Subtract<A, N> extends never
  ? A['length']
  : ModulusOfArray<Subtract<A, N>, N>;

type Modulus<A extends number, N extends number> = ModulusOfArray<ToArray<A>, ToArray<N>>;

@teamchong
Copy link

teamchong commented Apr 3, 2024

Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants