Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad
#

Some user interactions, such as resizing and scrolling, can create a huge number of browser events in a short period of time. If listeners attached to these events take a long time to execute, the user's browser can start to slow down significantly. To mitigate this issue, we want to to implement a throttle function that will detect clusters of events and reduce the number of times we call an expensive function.

Your function will accept an array representing a stream of event timestamps and return an array representing the times that a callback should have been called. If an event happens within wait time of the previous event, it is part of the same cluster. Your function should satisfy the following use cases:

  1. Firing once on the first event in a cluster, e.g. as soon as the window starts resizing.
  2. Firing once after the last event in a cluster, e.g. after the user window stops resizing.
  3. Firing every interval milliseconds during a cluster, e.g. every 100ms while the window is resizing.
function throttle(wait, onLast, onFirst, interval, timestamps) {
  let ret = [];
  let cluster = [timestamps[0]];
  
  for(let i=1; i<timestamps.length; i++) {
    if(timestamps[i] - timestamps[i-1] <= wait) {
      cluster.push(timestamps[i]);
    } else {
      let clusterEventTimes = evaluateCluster(wait, onLast, onFirst, interval, cluster);
      clusterEventTimes.forEach( function(el){ ret.push(el); });
      cluster = [timestamps[i]];
    }
    
    if(i == timestamps.length-1) {
      let clusterEventTimes = evaluateCluster(wait, onLast, onFirst, interval, cluster);
      clusterEventTimes.forEach( function(el){ ret.push(el); });
    }
  }
  
  return ret;
}

// Determines all times when an event needs to be fired
function evaluateCluster(wait, onLast, onFirst, interval, cluster){
  let ret = [];
  
  if(onFirst) {
    ret.push(cluster[0]); // push cluster start
  }
  if(interval != 0) {
    let maxInterval = cluster[cluster.length-1];
    if(onLast) {
      maxInterval += wait;
    }
    for(let intEv = cluster[0]+interval; intEv < maxInterval; intEv+=interval) {
      ret.push(intEv);
    }
  }
  if(onLast) {
    ret.push(cluster[cluster.length-1]+wait); // push cluster end
  }
    
  return ret;
}

Set 1 Challenge 1 for the cryptopal crypto challenges (https://cryptopals.com/sets/1/challenges/1).

Convert hex to base64

function hexToBase64(hex) {
  return hex;
}
doomsbyFailed Tests

Pairs

You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to the target value.

For example, given an array of [1,2,3,4] and a target value of 1, we have three pairs meeting the condition. 2 - 1 = 1, 3 - 2 = 1, and 4 - 3 = 1.

Input Format:

The first line contains n and k, the number of array elements and the target value.
The second line contains n space-separated integers of the array.

Constraints:

2 ≤ n ≤ 105
0 < k < 109
each integer element arr[i], will conform to 0 < arr[i] < 2^31 - 1
each integer arr[i] will be unique
Output Format
An integer representing the number of pairs of integers whose difference is k.

Sample Input:

5, 2, [1, 5, 3, 4, 2]

Sample Output:

3

Explanation:

There are 3 pairs of integers in the set with a difference of 2: [5,3], [4,2], and [3,1].

function pairs(num_elements, target_value, array) {
  
  return 0;
}

TIL that

instance Foldable ((,) a) where
    foldMap f (_, y) = f y
    foldr f z (_, y) = f y z
module FoldableTuple2 where
-- see the tests

Why when i try to run it i get the EOFError? I don't get it when i test in Python 3.6.5 Shell

from datetime import datetime
a = datetime.now()
c = str(a.hour) + str(a.minute)
b = input("Please, print the password here (without letters!): ")
if b.isdigit():
    if(b == c):
        print("ACCESS CONFIRMED")
    else:
        print("ACCESS DENIED")
else:
    print("Please, don't print letters.")
    print("Reset the program.")
#include <stdlib.h>

typedef struct IntVector {
  int *data;
} IntVector;

IntVector *IntVector_new(size_t size) {
  IntVector *this = malloc(sizeof(IntVector));
  this->data = calloc(sizeof(*this->data), size);
  return this;
}

void IntVector_free(IntVector *this) {
  free(this->data);
  free(this);
}

int IntVector_get(const IntVector *this, size_t index) {
  return this->data[index];
}

void IntVector_set(IntVector *this, size_t index, int value) {
  return this->data[index] = value;
}

Yes and no. A queue can be implemented using one explicit stack by leveraging the technique of recursion in the "dequeue" operation but note that recursion is handled internally by a function call stack; hence, technically, there are still two stacks at work ;)

class Queue {
  constructor() {
    this._stack = []; // We will be using this array as a stack - only
    // its push and pop operations will ever be called
  }
  enqueue(data) {
    this._stack.push(data);
  }
  dequeue() {
    if (this._stack.length === 1)
      return this._stack.pop();
    else {
      var tmp = this._stack.pop(), result = this.dequeue();
      this.enqueue(tmp);
      return result;
    }
  }
}
def array_of_two(n)
  raise 'n is not a number' unless n.is_a?(Numeric)  # comment to fail test case
  [n, n]
end

Just testing whether it is possible to "cheat" using the stack internally maintained by the computer to "implement" stack operations (between calls) in NASM.

Conclusion: It doesn't seem to be possible - at least with the way I did it, I got a segfault during the first call to stack_push() which just attempts to push its second argument to the internal stack and returns to the caller, let alone the pop and peek operations. There also doesn't seem to be a reliable way to check whether the internal stack is empty. Thus, it should be safe to translate my recent Stacks kata into NASM.

global stack_push, stack_pop, stack_peek ; , stack_is_empty
section .text
stack_push:
  push rsi
  ret
stack_pop:
  pop rsi
  ret
stack_peek:
  pop rsi
  push rsi
  ret