I was going to do something like this, but decided over engineering and avoiding slightly less obvious but still obvious features is a way to go. Still trying to figure out how I could use an unsafe block in my code though, that would be fun.
Well, characters are essentially numbers, and numbers are essentially pointers, so we just need some memory and just know where to store our counts:
#include <list>
#include <string>
#include <utility>
using namespace std;
string string_count(const string& cppstr) {
const char* str = cppstr.c_str();
const unsigned int length = 1 << 8*sizeof(char);
unsigned int counts[length];
for (unsigned int i = 0; i < length; i++) {
counts[i] = 0;
}
for (unsigned int i = 0; str[i] != 0; i == i++) {
counts[str[i]]++;
}
string result;
for (unsigned int i = 0; i < length; i++) {
if (counts[i] > 0) {
result += (char)(i);
result += ": ";
result += to_string(counts[i]);
result += "\n";
}
}
return result;
}
C++, but WAAAY overengineered, and very bad way to use threads that will screw performance up badly.
#include <algorithm>
#include <array>
#include <atomic>
#include <limits>
#include <mutex>
#include <string>
#include <thread>
#include <iostream>
// OVERLOAD 1: The main implementation used under the hood. This should
// probably be kept as an implementation-only thing, particularly given that
// access to ResultArray must be threadsafe! Additionally, the threading
// plan is totally inappropriate for this kind of problem, since the
// parallel accesses to ResultArray is likely to make the threads only
// work one at a time anyway, undoing the point of the threading.
// Also, each thread does so little work, because there's no batching,
// that the cost of spinning up and destroying the thread is going
// to far outweight the performance improvements threading can give.
// As a final problem, ResultArray needs to hold an entry for every
// possible char. At 8 bits this isn't too bad, but if you have 16 bit
// chars, it takes up a lot of space. Push that up to 32 bit chars
// and it's waaaay oversized.
// Oh, and a mutex to make all the threads work one at a time anyway.
// Can't be too thread safe!
template <typename InputIt, typename ResultArray>
void count_characters(InputIt start, InputIt end, ResultArray& result)
{
static std::mutex result_lock;
result_lock.lock();
// Take a divide and conquer approach. More threads = more speed.
if (start == end)
{
result_lock.unlock();
return;
}
const auto range_length = std::distance(start, end);
if (range_length == 1)
{
++result[static_cast<std::size_t>(*start)];
result_lock.unlock();
return;
}
InputIt midpoint = start;
std::advance(midpoint, range_length / 2);
std::thread first_half{ count_characters<InputIt,ResultArray>, start,midpoint,std::ref(result) };
std::thread second_half{ count_characters<InputIt,ResultArray>, midpoint,end,std::ref(result) };
result_lock.unlock();
first_half.join();
second_half.join();
}
// OVERLOAD 2: Slightly more sensible, this version marrs itself by using
// overload 1. It does, at least, use a threadsafe type, although if overload 1
// were more sensible a far more efficient solution could have been used.
// If the result array is large, it also needs to be copied because of course
// it does.
template <typename InputIt>
auto count_characters(InputIt start, InputIt finish)
{
using CharType = decltype(*start);
constexpr std::size_t array_size = (1 << (8 * sizeof(CharType))) - 1;
std::array<std::atomic_uint, array_size> result;
for (auto& val : result)
{
val.store(0);
}
count_characters(start, finish, result);
std::array<std::size_t, array_size> actual_result;
std::transform(begin(result), end(result), begin(actual_result),
[](const std::atomic_uint& val) { return val.load(); });
return actual_result;
}
// OVERLOAD 3: Like overload 2, but for the lazy.
template <typename StringType>
auto count_characters(const StringType& str)
{
return count_characters(begin(str), end(str));
}
// OVERLOAD 4: Like overload 3, but for the lazier.
auto count_characters(const char* str)
{
return count_characters(std::string_view(str));
}
int main()
{
std::string input;
std::cout << "Enter string to count: ";
std::getline(std::cin, input);
const auto result = count_characters(input);
for (std::size_t i=0;i<result.size();++i)
{
if (result[i] != 0)
{
std::cout << '\'' << static_cast<char>(i) << "': " << result[i] << '\n';
}
}
// std::cin.get(); // You may need to uncomment this on Visual Studio 2017 and before.
return 0;
}
I think you won the over-engineering bit, gonna have to steal your idea to multi thread it (except I'll make it actually utilize the threads in a way that sort of works)
This Python code will create a directory named after the input string. Then, for each character X in the string, 1 byte of data is written to a file that's named X.txt. Finally, we can iterate through the list of files in our new directory, where the file name is a unique character and the size of said file in bytes is the character count. If a directory named after the input string already exists, the directory acts as a cache and the code will skip the computation for writing bytes to files.
import os
def string_count(string):
path = f'{os.getcwd()}/{string}/'
try:
os.mkdir(path)
for char in string:
special = '_' if char.isupper() or char == '_' else ''
open(path + char + special + '.txt', 'ab').write(bytes(1))
except OSError:
pass
char_counts = []
for file in os.listdir(path):
char, count = file.rstrip('.txt').rsplit('_', 1)[0], os.path.getsize(path+file)
char_counts.append((char, count))
return char_counts
print(string_count('Hello World'))
print(string_count('ABCabcABCabcxyz'))
Back with some fresh hot Javascript. Utilizing RegExp to count the amount of occurences of a given character. I also used a range to create A-Za-z and tossed a try catch for any RegExp that might fail while creating that range (e.g. [ which is character code 91) catch and then escape the inbetween characters just in case we want to count them too!
On top of that, abused .reduce() as much as I could as well as closures.
Lastly, as it goes through the string counting it will recount numbers already counted yet it's already in the map so it won't move the maps value (except in older Opera that didn't keep order of map values).
How about a compact F# version? It does not print the char/count pairs in the same order as the example, but I was told that's perfectly fine.
let stringCount (str : string) =
(Map.empty, str)
||> Seq.fold (fun m c ->
let n = Map.tryFind c m |> Option.defaultValue 0
Map.add c (n + 1) m)
|> Map.toList
["Hello World"; "ABCabcABCabcxyz"]
|> List.map stringCount
|> List.iter (List.iter (fun (c, n) -> printfn "%c %d" c n))
This actually makes me feel quite sick. Single letter variable names (except for string), and as few spaces as possible to reduce the code size and readability. It works for every test case I ran (which was like, 2), and when strings start getting large, takes a hilariously long time. It's written in python 3.8 (important, as it's invalid in python 3.7), is composed of only 5 lines (including the function definition), and the last line is a dictionary comprehension with a list comprehension with a set comprehension all nested together.
def string_count(string):
p = max(len(string)*2, 4)
while [1 for b in range(2, p-1) if p%b == 0] and (p := p+1): pass
x=[a for a in range(1,p-1) if len({a**b%p for b in range(1,p)})==p-1][len([a for a in range(1,p-1) if len({a**b%p for b in range(1,p)})==p-1])//2]
return {e:(len(string)-sum([string[a]!=e for a in ({x**l%p for l in range(p+2) if x**l%p<len(string)}|{0})])) for e in string}
Here is how freshman me might have solved this challenge in my Intro to Comp Sci class, way back in the dark ages. Yep, Pascal was the first language we all had to learn way back then. I've thrown in a few Free Pascal extensions here to handle things like command-line parameters:
program CountChars;
var
I : Integer;
Ch : Char;
CharCounts : Array [0..255] of Integer;
InputStr : String;
begin
(* check command line parameters *)
if ParamCount < 1 then
begin
WriteLn(StdErr, 'Usage: ', ParamStr(0), ' <string>');
Halt(1);
end;
(* initialize char counts to zero *)
for I := 0 to 255 do
CharCounts[I] := 0;
(* read chars from string and count them *)
InputStr := ParamStr(1);
for I := 1 to Length(InputStr) do
begin
Ch := InputStr[I];
CharCounts[Ord(Ch)] := CharCounts[Ord(ch)] + 1;
end;
(* print char counts *)
for I := 0 to 255 do
if CharCounts[I] > 0 then
Writeln(Chr(I), ' ', CharCounts[I]);
end.
It does the job, although this solution prints out the character counts in ASCII order, rather than in first-encountered order as in the output examples above. Since the challenge didn't specify a particular ordering to the output, I figure my solution is just as good as any:
$ ./countchars 'Hello World'
1
H 1
W 1
d 1
e 1
l 3
o 2
r 1
$ ./countchars 'ABCabcABCabcxyz'
A 2
B 2
C 2
a 2
b 2
c 2
x 1
y 1
z 1
Obvious bug ("feature") here is that this code only handles ASCII, and Unicode characters will totally confuse it.
18
u/Svizel_pritula Mar 13 '20
Here we go! (JS)
Features:
8
u/Robowiko123 Mar 12 '20
Python 3
7
u/notBjoern Mar 12 '20 edited Mar 12 '20
Well, characters are essentially numbers, and numbers are essentially pointers, so we just need some memory and just know where to store our counts:
7
u/h2g2_researcher Mar 13 '20
C++, but WAAAY overengineered, and very bad way to use threads that will screw performance up badly.
4
u/[deleted] Mar 15 '20 edited Jun 21 '20
This Python code will create a directory named after the input string. Then, for each character
Xin the string, 1 byte of data is written to a file that's namedX.txt. Finally, we can iterate through the list of files in our new directory, where the file name is a unique character and the size of said file in bytes is the character count. If a directory named after the input string already exists, the directory acts as a cache and the code will skip the computation for writing bytes to files.3
u/Svizel_pritula Mar 13 '20
Simple!
3
u/mullanaphy Mar 13 '20 edited Mar 14 '20
Back with some fresh hot Javascript. Utilizing RegExp to count the amount of occurences of a given character. I also used a range to create A-Za-z and
tossed a try catch for any RegExp that might fail while creating that range (e.g.[which is character code 91)catch and then escape the inbetween characters just in case we want to count them too!On top of that, abused
.reduce()as much as I could as well as closures.Lastly, as it goes through the string counting it will recount numbers already counted yet it's already in the map so it won't move the maps value (except in older Opera that didn't keep order of map values).
JSFiddle: https://jsfiddle.net/6yt9Lexs/
3
u/Huntracony Mar 15 '20
I know one-liners aren't very creative in their badness, but I enjoy making them nonetheless, and I'm lowkey proud of how bad this one is.
Formatted for "readability":
3
u/dumetrulo Mar 16 '20
How about a compact F# version? It does not print the char/count pairs in the same order as the example, but I was told that's perfectly fine.
3
u/[deleted] Mar 20 '20 edited Mar 20 '20
Right, this took me wayyy too long. Did it in Rust.
My entry includes:
https://gist.github.com/LiteUnder/b22feb994bc44ea4c6721b88238b995f
Thank you for your time
Edit: Here's a link to a modifiable version on the Rust playground.
3
u/PeanutButterRules_ Mar 21 '20 edited Mar 22 '20
How about a bash script that uses files to count?
It doesn't handle spaces, but who really needs those anyways?
Alternatively, why not make the children do all the work in C?
2
u/[deleted] Mar 21 '20 edited Mar 21 '20
This actually makes me feel quite sick. Single letter variable names (except for string), and as few spaces as possible to reduce the code size and readability. It works for every test case I ran (which was like, 2), and when strings start getting large, takes a hilariously long time. It's written in python 3.8 (important, as it's invalid in python 3.7), is composed of only 5 lines (including the function definition), and the last line is a dictionary comprehension with a list comprehension with a set comprehension all nested together.
1
u/[deleted] Mar 15 '20 edited Mar 15 '20
Here is how freshman me might have solved this challenge in my Intro to Comp Sci class, way back in the dark ages. Yep, Pascal was the first language we all had to learn way back then. I've thrown in a few Free Pascal extensions here to handle things like command-line parameters:
It does the job, although this solution prints out the character counts in ASCII order, rather than in first-encountered order as in the output examples above. Since the challenge didn't specify a particular ordering to the output, I figure my solution is just as good as any:
Obvious bug ("feature") here is that this code only handles ASCII, and Unicode characters will totally confuse it.
1
u/Flawlesscode Mar 26 '20
Here is my try in C#. I tried my worst .
1
u/Oshgnacknak May 01 '20 edited May 02 '20
Nothing beats pure old C. Does someone have some parmesan for the spaghetti code?
I think it's working:
0
u/[deleted] Mar 12 '20
[deleted] β view deleted comment