Coding Challenges

Last thread:

Here's a good one.
Write a shell script that can take an arbitrary number of words from a space-delimited list and print them back in reverse order.
No bashisms.

Future Js pro here.

This was pretty easy but I can't even get repl.it to run for a max number as low as 1000
for (var num = 2, abundant = []; num < 1000; num++){
for (var i = 1, properDivisors = []; i < num; i++){
if (String(num / i).indexOf(".") > -1){properDivisors.push(i)}
}
if (properDivisors.reduce(function(a,b){return a + b}) > num){
abundant.push(num);
}
}


After that I'd do a single nested loop to run through abundant to find any number that's off limits and I'd post every number but that.

Doing that in Js is too easy and idk know shell

I don't feel like bothering with efficiency as if repl.it can't even handle low numbers (this is the first time I've broken repl without doing anything wrong. 1000 is a low number)

An easy one:
From a given string, make a list of all vowels in the string and print the last one.

Here's an example in Python:
string = "Niggers tongue my anus"
vowels = ["A", "E", "I", "O", "U"]
strVowels = []
for i in string:
if i.upper() in vowels:
strVowels.append(i)

print(strVowels[len(strVowels) - 1])

echo "$@" | rev

Hmm.... Let's see.
I'll do this in C.
Make a shell.

>arrays aren't a POSIX shell feature

fuck this

This kind of thing is too easy
var vowels = "aeiou";

string.split("").filter(function(l){return l.toLowerCase().indexOf(vowels) > -1}).
reduce(function(a,b){return b})

>shell
Maybe later.
var input = "poo loo paj eet";

WriteLine(Join(" ", input.Split(' ').Reverse()));

In C#:
var input = "Niggers tongue my anus";

WriteLine(input.Where(x => new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(x)).Last());

Y'all niggas got some dumb challenges.

Second one here should have a .ToLower() at some point, but meh.

Write a code that takes a string of a sentence and write whether or not the letters in the string are palindromic.

string = '"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots." '

string = string.toLowerCase().split(" ").
filter(function(l){return l.charCodeAt() >= "a".charCodeAt() &&
l.charCodeAt()

>C#
I want to compete against other Js people because this is bullshit

C#:
var input = "\"Stop!\" nine myriad murmur. \"Put up rum, rum, dairymen, in pots.\"";

var inputStripped = input.ToLower().Where(x => char.IsLetter(x));

WriteLine(Enumerable.SequenceEqual(inputStripped, inputStripped.Reverse()));

#define static_size(p) (sizeof(p) / sizeof(*p))
int is_palindrome(const char *str)
{
char *arr[2]; /* buffer */
unsigned len = strlen(str);
unsigned i, idx = 0, matches = 0;
for (i = 0; i < static_size(arr); i++)
arr[i] = (char *) malloc(sizeof(char) * len + 1);
for (i = 0; i < len; i++)
{
char c = str[i];
c -= (c >= 'a' && c = 'A' && c

Good old C.
#include

void main(){

char mystring[] = "I know how to programme.";
int stringlength =
sizeof(mystring) / sizeof(char);
char stringvowles[stringlength];
int x, y;
for(x = 0; x < stringlength; x++)
{
if (mystring[x] == 'a' ||
mystring[x] == 'e' ||
mystring[x] == 'I' ||
mystring[x] == 'o' ||
mystring[x] == 'u') {
stringvowles[x] = mystring[x];
y = x; }
}
printf("%c\n", stringvowles[y]);
}

string = 'wewladaeiou'
print(*filter((lambda a: a.lower() in 'aeiou'), string[::-1]))

c autists will defend this

Given a sequence of n real numbers A(1) ... A(n), determine a subsequence (not necessarily contiguous) of maximum length in which the values in the subsequence form a strictly increasing sequence.

$s = '"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots."';
if($s eq reverse($s)) says "Palindrome";

Lua
local vowels = {["A"] = true, ["E"] = true, ["I"] = true, ["O"] = true, ["U"] = true}
function vowelCount(phrase)
local vowelList = {}
for i = 1, string.len(phrase) do
local letter = string.upper(string.sub(phrase, i, i))
if vowels[letter] then
table.insert(vowelList, letter)
end
end
return vowelList
end

local vowelsInPhrase = vowelCount("Niggers tongue my anus")
print("Vowels: " .. tostring(#vowelsInPhrase))
print(vowelsInPhrase[#vowelsInPhrase])

Output:
Vowels: 7
U

I don't think that solves the intent of the question.

Judging by the sentence, the asker likely wanted the function to ignore punctuation, as this sentence is clearly constructed to be palindromic when only the alphanumeric characters are considered.

Interestingly enough, you may make the case that it wasn't clear, but this is what would prevent you from being a good team member.

In many cases, you're supposed to provide a good solution for the most likely problem, not create a petty, hyper-specific solution that ignore human error.

Fuck, posted the wrong one.


Correct:
string = 'wewladaeiou'
print([*filter((lambda a: a.lower() in 'aeiou'), string)][-1])

Too lazy to make it so that it reads the standard input in case argc = 1 but whatev.
#include
#include
#include
#include

int main(int argc, char *argv[])
{
if (argc == 1)
printf("Usage: %s str\n", argv[0]);
else {
size_t total_length = 0;
for (int arg = 1; arg < argc; ++arg) {
total_length += strlen(argv[arg]);
}
char *vowels = malloc(total_length * sizeof(char));
if (!vowels) {
fputs("Allocation failure.", stderr);
exit(EXIT_FAILURE);
}
vowels[0] = 0;
size_t it = 0;
for (int arg = 1; arg < argc; ++arg) {
size_t length = strlen(argv[arg]);
for (size_t i = 0; i < length; ++i) {
switch (tolower(argv[arg][i])) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels[it] = argv[arg][i];
vowels[++it] = 0;
}
}
}
if (it)
printf("%c\n", vowels[it - 1]);
else
puts("No vowels found.");
free(vowels);
}
return EXIT_SUCCESS;
}

Lua
function isPalindrome(phrase)
local phraseMin = ""
for i = 1, string.len(phrase) do
local letter = string.upper(string.sub(phrase, i, i))
if (letter >= "A") and (letter

It's probably just shitty code. Solving these challenges is a skill unto itself.

All C is shitty code, in some respects.

Necessary evil, but absolute cancer to work with.

$s = '"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots."';
$s =~ s/\s+|\W+//g;
if($s eq reverse($s)) says "Palindrome";

Is that better?

whoops
$s = '"Stop!" nine myriad murmur. "Put up rum, rum, dairymen, in pots."';
$s =~ s/\s+|\W+//g;
if(lc $s eq reverse(lc $s)) print"Palindrome!";

Forgot to lowercase it

Let me edit this

var string = "\"Stop!\" nine myriad murmur. \"Put up rum, rum, dairymen, in pots.\""

string = string.toLowerCase().split("").filter(function(l){return l !== l.toUpperCase()})

string === string.reverse()


I was stupid for using character codes

I'm wondering if you're adept at these coding challenges or if C# just has great syntax

>I'm wondering if you're adept at these coding challenges or if C# just has great syntax
The latter, really.

C# has comfy as fuck syntax.

It's certainly not python golf-tier for shortness, but it's short while still being very readable.

Once you learn LINQ, you pretty much use it for everything.

And we're getting native tuples, pattern matching, and in-line functions in C# 7, so it's only getting better.

Too easy.
#include
#include
#include
#include

#define MAX_INPUT 200

int main(int argc, char *argv[])
{
char input[MAX_INPUT + 1];
switch (argc) {
case 1:
fgets(input, MAX_INPUT, stdin);
char *nl = strchr(input, '\n');
if (nl)
*nl = 0;
else
input[MAX_INPUT] = 0;
break;
case 2:
strcpy(input, argv[1]);
break;
default:
printf("Usage: %s [str]\n", argv[0]);
exit(EXIT_FAILURE);
}
size_t i = 0;
size_t j = strlen(input) - 1;
while (!isalpha(input[i]))
++i;
while (!isalpha(input[j]))
--j;
char palyndromic = 1;
while (i < j) {
if (tolower(input[i]) != tolower(input[j])) {
palyndromic = 0;
break;
} else {
do
++i;
while (!isalpha(input[i]));
do
--j;
while (!isalpha(input[j]));
}
}
if (palyndromic)
puts("breddy gud :^)");
else
puts("smhtbhfam :^(");
return EXIT_SUCCESS;
}

Absolutely disgusting.

Like massive collagen-infested landwhales, your code has managed to be both bloated and unintuitive to read.

What could I have done differently
>lua user

That image depicts an SQL injection vulnerability.

kill yourself'; DROP TABLE 4chanpasses;--

$s = "Niggers tongue my anus";
@s = $s =~ m/[aeiouy]/ig;
print @s[-1];

My fingers are fat and stupid

I have no idea, but no modern language should take more than 3-5 lines for such a basic operation.

I understand you did a little boilerplate with the function and all, but without the function name, it's not immediately apparent as to what you're doing.

Why not use a language that almost lets you see instantly that you're just stripping non-alphanumeric, and comparing it to the reverse of itself?

>in-line functions
Explain

Not necessarily, Bobby.

You can't see the rest of the code handling the input of that value.

Also, even if it WAS injectable, the database service account (or even intentionally spoofed user account via a login) used for the application may have very specific rights that would prevent unauthorized access or modification anyway.

>secured input, the standard library and a proper argv usage are bloated
Found the Python software engineer.

I'm the Js guy but in my opinion, if you're looping then that's a failure and if you're using if statements, it's really a failure.

Multiple return statements makes it look like your logic can't support itself. That kind of thing.

In pic related, logsword() is a function declared within the scope of the function I'm already working in.

It's a basic, simple thing, but it allows you to make tiny little single-purpose functions if you only need something for a specific context.

Technically speaking, it can improve performance because you wouldn't have to allocate any new space for objects passed to the function in some situations, but that's mostly an edge case.

In this function, the Sharpen() extension method is actually using a basic form of pattern matching to handle the logic:
public static void Sharpen(this Sword s)
{
switch (s)
{
case Rapier r when r.Durability > 10:
r.Durability -= 10;
r.Damage += 3;
break;
case Zweihander z when z.Durability > 10:
z.Durability -= 10;
z.Damage += 2;
break;
case Sword sw when sw.Durability

>Multiple return statements makes it look like your logic can't support itself
What? How so?

Not that user, but I'm think you should only have one return statement.

It provides a clear exit point for the method, which makes it much easier to reason about later, or if you're a maintainer.

>mystring[] = "I know how to programme."
>sizeof(mystring) / sizeof(char);

you fucked up didn't you

Ah, I suppose so. Never really worked on large projects, so I wouldn't know. Makes sense though.

As to the "logic can't support itself" comment, I think there's some merit there.

A method should come to a logical conclusion based on the input, and multiple outcomes means that you may be looking at a case where you're taking the easy way out of a default or edge case, rather than handling it.

God forbid you're returning in a catch block.

Try.
{
Write: 'Not being a fag'.
}
Catch.
{
Exception A.
}
Endtry.

In python there is another way to print the last thing in a list. You can use name_of_list[-1] to print the last element in the list. Also name_of_list[::-1] will print the list backwards( including strings )

The more you know

...

He didn't. It's an array and not a pointer.

Use strpbrk for this kind of thing.

int main(int argc, char **argv)
{
char str[] = "NIGGERS TONGUE MY ANUS";
char vowels[] = "aeiou";
char *vowel;
size_t i;
for (i = 0; i < strlen(str); ++i) {
str[i] = tolower(str[i]);
}
vowel = strpbrk(str, vowels);
while (vowel != NULL){
printf("%c", *vowel);
vowel = strpbrk(vowel + 1, vowels);
}
return 0;
}

Another C implementation.
int main(int argc, char **argv)
{
char str[] = "\"Stop!\" nine myriad murmur. \"Put up rum, rum, dairymen, in pots.\"";
char *stripped_str = (char*)malloc(strlen(str) + 1);
memset(stripped_str, '\0', strlen(stripped_str));
char *beg = stripped_str;
char *end;
bool palindrome = false;
size_t i;
for (i = 0; i < strlen(str); ++i)
{
if (isalpha(str[i]) && !isspace(str[i])) {
*beg++ = tolower(str[i]);
}
else {
continue;
}
}
beg = stripped_str;
end = stripped_str + strlen(stripped_str) - 1;
while (beg < end) {
if (*beg != *end) {
palindrome = false;
break;
}
beg++;
end--;
palindrome = true;
}

if (palindrome) {
printf("\"%s\" is a palindrome.\n", stripped_str);
}
else {
printf("\"%s\" is not a palindrome.\n", stripped_str);
}
return 0;
}

...

I'm going to post what I have here. Maybe someone has others, if they exist.

...

...

...

In Rust:

fn main() {
let string = "Niggers tongue my anus";
let vowels = ['a', 'e', 'i', 'o', 'u'];

let all_vowels = string.chars()
.flat_map(|c| c.to_lowercase())
.filter(|c| vowels.contains(c));

if let Some(l) = all_vowels.last() {
println!("{}", l);
}
}

rolling so I can choose between 4

ing of abn
broodje pindakaas met jam

Create a program that iterates through the index of pi to a certain point > 48

At each index spell out the number.
Don't have to include the decimal point.

Wtf are you doing my man

Implement the following sorting algorithm for ints:

Randomly permute the input and check if the result is sorted. Repeat this process until the result is sorted. You must implement the random permutation algorithm yourself.

Write a program that prints all contiguous substrings of a given string

rolio

./script -n
> returns nothing
F minus

>#define static_size(p) (sizeof(p) / sizeof(*p)
For what reason?
What do we want to get?

Let's see if I can even do what I roll.

k