language-agnostic

Algorithm for joining e.g. an array of strings

大兔子大兔子 提交于 2020-01-10 18:16:46
问题 I have wondered for some time, what a nice, clean solution for joining an array of strings might look like. Example: I have ["Alpha", "Beta", "Gamma"] and want to join the strings into one, separated by commas – "Alpha, Beta, Gamma". Now I know that most programming languages offer some kind of join method for this. I just wonder how these might be implemented. When I took introductory courses, I often tried to go it alone, but never found a satisfactory algorithm. Everything seemed rather

Algorithm to find if two sets intersect

不打扰是莪最后的温柔 提交于 2020-01-10 18:11:09
问题 Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285}; int ArrayB[] = {7, 11, 57, 110, 230, 250}; Both arrays are sorted and can be any size. I am looking for an efficient algorithm to find if the arrays contain any duplicated elements between them. I just want a true/false answer, I don't care which element is shared or how many. The naive solution is to loop through each item in ArrayA, and do a binary search for it in ArrayB. I believe this complexity is O(m * log n). Because

Why can you only prepend to lists in functional languages?

北慕城南 提交于 2020-01-10 17:30:27
问题 I have only used 3 functional languages -- scala, erlang, and haskell, but in all 3 of these, the correct way to build lists is to prepend the new data to the front and then reversing it instead of just appending to the end. Of course, you could append to the lists, but that results in an entirely new list being constructed. Why is this? I could imagine it's because lists are implemented internally as linked lists, but why couldn't they just be implemented as doubly linked lists so you could

Code Golf - Banner Generation

那年仲夏 提交于 2020-01-10 06:59:28
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. When thanking someone, you don't want to just send them an e-mail saying "Thanks!", you want to have something FLASHY: Input: THANKS!! Output: TTT H H AAA N N K K SSS !!! !!! T H H A A NNN K K S !!! !!! T HHH AAA NNN KK SSS !!! !!! T H H A A N N K K S T H H A A N N K K SSS !!! !!! Write a program to generate a banner.

Sorting: how to sort an array that contains 3 kind of numbers

左心房为你撑大大i 提交于 2020-01-09 19:58:53
问题 For example: int A[] = {3,2,1,2,3,2,1,3,1,2,3}; How to sort this array efficiently? This is for a job interview, I need just a pseudo-code. 回答1: Problem description: You have n buckets, each bucket contain one coin , the value of the coin can be 5 or 10 or 20. you have to sort the buckets under this limitation: 1. you can use this 2 functions only: SwitchBaskets (Basket1, Basket2) – switch 2 baskets GetCoinValue (Basket1) – return Coin Value in selected basket 2. you cant define array of size

determine if line segment is inside polygon

流过昼夜 提交于 2020-01-09 10:57:20
问题 suppose we have convex polygon with vertices (v0,v1,....vn) my aim is to determine if for given point p(x,y) any line segment connecting this point and any vertices of polygon is inside polygon or even for given two point p(x0,y0) `p(x1,y1)` line segment connecting these two point is inside polygon? i have searched many sites about this ,but i am still confused,generally i think we have to compare coordinates of vertices and by determing coordinates of which point is less or greater to

Secret santa algorithm

梦想的初衷 提交于 2020-01-09 06:18:50
问题 Every Christmas we draw names for gift exchanges in my family. This usually involves mulitple redraws until no one has pulled their spouse. So this year I coded up my own name drawing app that takes in a bunch of names, a bunch of disallowed pairings, and sends off an email to everyone with their chosen giftee. Right now, the algorithm works like this (in pseudocode): function DrawNames(list allPeople, map disallowedPairs) returns map // Make a list of potential candidates foreach person in

Secret santa algorithm

泄露秘密 提交于 2020-01-09 06:18:12
问题 Every Christmas we draw names for gift exchanges in my family. This usually involves mulitple redraws until no one has pulled their spouse. So this year I coded up my own name drawing app that takes in a bunch of names, a bunch of disallowed pairings, and sends off an email to everyone with their chosen giftee. Right now, the algorithm works like this (in pseudocode): function DrawNames(list allPeople, map disallowedPairs) returns map // Make a list of potential candidates foreach person in

What is an off-by-one error and how do I fix it?

佐手、 提交于 2020-01-09 05:05:21
问题 What is an off-by-one error? If I have one, how do I fix it? 回答1: An off-by-one error is for example when you write intend to perform a loop n times and write something like: for (int i = 1; i < n; ++i) { ... } or: for (int i = 0; i <= n; ++i) { ... } In the first case the loop will be executed (n - 1) times and in the second case (n + 1) times, giving the name off-by-one. Other variations are possible but in general the loop is executed one too many or one too few times due to an error in

Should we compare floating point numbers for equality against a *relative* error?

吃可爱长大的小学妹 提交于 2020-01-09 02:21:26
问题 So far I've seen many posts dealing with equality of floating point numbers. The standard answer to a question like "how should we decide if x and y are equal?" is abs(x - y) < epsilon where epsilon is a fixed , small constant. This is because the "operands" x and y are often the results of some computation where a rounding error is involved, hence the standard equality operator == is not what we mean, and what we should really ask is whether x and y are close , not equal. Now, I feel that if