Questions tagged [counting]

For challenges regarding counting the number of occurrences of some characteristic.

Filter by
Sorted by
Tagged with
85 votes
209 answers
15k views

Count up forever

Write a program that counts up forever, starting from one. Rules: Your program must log to STDOUT or an acceptable alternative, if ...
  • 2,463
85 votes
9 answers
23k views

Counting Grains of Rice

Consider these 10 images of various amounts of uncooked grains of white rice. THESE ARE ONLY THUMBNAILS. Click an image to view it at full size. A: B: C: D: E: F: G: H: I: J: Grain Counts: <...
66 votes
60 answers
4k views

Count trailing truths

Inspired by, and in memory of, my dear friend and colleague, Dan Baronet, 1956 – 2016. R.I.P. He found the shortest possible APL solution to this task: Task Given a Boolean list, count the number of ...
  • 29k
61 votes
26 answers
5k views

Cantor's unspeakable numbers

An unspeakable number is a number which is divisible by seven or has seven as one of its digits. A children game is to count skipping unspeakable numbers ...
  • 1,727
53 votes
42 answers
5k views

Stretch the word

The input is a word of lowercase letters not separated by whitespace. A newline at the end is optional. The same word must be output in a modified version: For each character, double it the second ...
  • 1,171
53 votes
33 answers
11k views

Avoid the evil integers! [closed]

You are developing some code to generate ID numbers. Policy requires that no ID numbers include the digit sequence 666. Create a function (or your language's equivalent) which takes a positive ...
  • 2,055
50 votes
28 answers
9k views

Covering a Skyline with brush strokes

Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it. [1,3,2,1,2,1,5,3,3,4,2]...
  • 29k
48 votes
14 answers
4k views

Molecules to Atoms

The Challenge Write a program that can break down an input chemical formula (see below), and output its respective atoms in the form element: atom-count. Input ...
  • 6,622
47 votes
20 answers
6k views

Into how many pieces can you cut this string?

Consider a piece of string (as in "rope", not as in "a bunch of characters"), which is folded back and forth on the real line. We can describe the shape of the string with a list of points it passes ...
46 votes
23 answers
9k views

Count down from "Infinity"

Seems like an impossible task right? Well, it's actually not that hard. If we write the word Infinity as 8-bit binary ASCII code, we'll get: ...
42 votes
27 answers
8k views

Fast, Cheap, and Good - Choose any Two

As programmers, we all know the saying: "You can have it fast and good, but it won't be cheap, you can have it cheap and good, but it won't be fast, or you can have it fast and cheap, but it won't be ...
42 votes
18 answers
6k views

Count to 100 in Danish

Print all the numbers from 0-100 in the right order using the Danish way of counting How they count Like English, they have dedicated words for 0-20, 30, 40 and 100 Instead of saying ...
  • 3,868
40 votes
40 answers
5k views

Detriplicate a string

A lot of languages have built-in ways to get rid of duplicates, or "deduplicate" or "uniquify" a list or string. A less common task is to "detriplicate" a string. That is,...
39 votes
24 answers
4k views

How many lights are on?

You are little speck on a Cartesian plane. Pondering your existence, you decide to roam around your plane and touch things. You notice that every coordinate pair you visit has a light switch. To ...
38 votes
58 answers
6k views

How much do I have to write?

Writing out numbers is among the Hello worlds of programming, often the numbers 1-10. I want to write out many numbers! Many, Many numbers. But how many numbers do I have to write? Task Given an ...
  • 3,613
38 votes
51 answers
10k views

Determine the depth of an array

A simple challenge for your Monday evening (well, or Tuesday morning in the other half of the world...) You're given as input a nested, potentially ragged array of positive integers: ...
38 votes
15 answers
5k views

How long is a Welsh word?

Write a program or function which receives as input a string representing a Welsh word (UTF-8 unless otherwise specified by you). The following are all single letters in Welsh: a, b, c, ch, d, dd, ...
  • 619
36 votes
24 answers
3k views

Plus Primes vs Minus Primes

Most of us know... that all primes p>3 are of the form But, how many are the Plus Primes (6n+1) and how many are the ...
user avatar
35 votes
24 answers
8k views

Checkmate (aka the urinal problem)

My Precalc teacher has one of his favorite problems that he made up (or more likely stole inspired by xkcd) that involves a row of n urinals. "Checkmate" is a ...
  • 669
34 votes
28 answers
3k views

Count rook moves 1D

Given a position with a row of rooks and/or empty spaces, output how many different rook moves are possible. A rook can move left or right to an empty space, but not to one that requires passing over ...
  • 141k
34 votes
22 answers
3k views

Alphabet Histogram

Given an input sentence consisting of one or more words [a-z]+ and zero or more spaces , output an ASCII-art histogram (bar ...
  • 43.1k
33 votes
60 answers
4k views

GET your dubs together

On 4chan, a popular game is get. Every post on the site gets a sequential post ID. Since you can't influence or determine them, people try to guess (at least a part of) their own post number, usually ...
  • 1,355
33 votes
29 answers
2k views

Say What You See

The "Look and say" or "Say what you see" sequence is a series of numbers where each describes the last. ...
  • 461
32 votes
25 answers
6k views

How many IP addresses are in a given range?

Inspired by... Networking - How can I work out how many IP addresses there are in a given range? Write a program or function that takes two strings as input, each being an IPv4 address ...
  • 2,055
32 votes
6 answers
2k views

Slash the matrix

You are given a matrix of forward and back slashes, for instance: //\\ \//\ //\/ A slash cuts along the diagonal of its cell corner-to-corner, splitting it in ...
  • 15.1k
31 votes
75 answers
12k views

Count the number of ones in an unsigned 16-bit integer

Write some statement(s) which will count the number of ones in an unsigned sixteen-bit integer. For example, if the input is 1337, then the result is ...
  • 1,009
29 votes
23 answers
6k views

A scene of Jimmy diversity

As you probably know, there have been multiple lovely Jimmy challenges recently popping up. In these challenges, you were challenged with our beloved friend's acrobatics skills. Now we've got a ...
  • 2,512
29 votes
25 answers
4k views

Robot on a ladder

Background I have a ladder leaning on a wall, and a remote-controlled robot that can climb it. I can send three different commands to the robot: UP: the robot ...
  • 42.3k
29 votes
9 answers
2k views

"You must construct additional pylons!"

Introduction In the strategy game Starcraft 2, there are three "races" to choose from: Terran, Zerg, and Protoss. In this challenge we will be focusing on the Protoss and the iconic phrase "You must ...
  • 7,494
27 votes
43 answers
2k views

Replace all items with their counts

Task: Given an array of numbers as input (you can choose what subset, such as integers or natural numbers), replace all items with the number of times they appear within the array. As an example, <...
26 votes
60 answers
4k views

Non-Unique/Duplicate Elements

Write a program which finds the non-unique elements of an array of signed integers. The resulting array can be in any order. Your answer may be a snippet which assumes the input to be stored in a ...
  • 689
26 votes
40 answers
2k views

Count /[^a-z]/ig with /[a-z]/ig

Write a program or function which takes a string of text as input and outputs the number of non-alphabetical characters in it (standard I/O rules apply). A non-alphabetical character is any character ...
26 votes
19 answers
1k views

Possible periods of input

Background: Take this input as an example: 1 1 2 1 1 2 1 3 1 3 If you look only at the first few digits, between 1 1 2 and <...
26 votes
12 answers
2k views

7-segment differences

I think most people around here know what a 7-segment display for digits is: ...
26 votes
10 answers
1k views

Number Lockers!

Given a positive integer < 100 (from 1 to 99, including 1 and 99), output that many lockers. A locker is defined as the following: ...
  • 6,692
25 votes
62 answers
6k views

Display number of occurrences for every character in an input string

The code should take a string as input from keyboard: The definition of insanity is quoting the same phrase again and again and not expect despair. The output ...
25 votes
41 answers
2k views

Replace 0s In a String With Their Consecutive Counts

Challenge Given a string of any length which contains only digits from 0 to 9, replace each consecutive run of the digit ...
user avatar
25 votes
43 answers
4k views

Character Frequency in a String [duplicate]

Given a string of printable ASCII, output the frequency of each character in that string. The Challenge Input is given as a string of printable ASCII characters (decimal ...
  • 6,182
25 votes
16 answers
2k views

Prime Wednesdays

Prime Wednesdays Your task is to count the number of Wednesdays that fall on a prime day of the month in a particular year. For instance, 7-13-16 is a prime ...
25 votes
27 answers
2k views

Count edits accounting for grace period

When you edit a post on SE, any further edits within a 5-minute grace period are merged into it. Given a list of times you edit a post, count the edits not in a grace period. Say you edit at minutes <...
  • 141k
25 votes
7 answers
936 views

Analyze the flow

On a toroidal square grid (you can wrap around) where each cell indicates one direction (^ > ...
  • 2,193
24 votes
5 answers
670 views

How great is your land?

In this challenge, you'll calculate how great your land is. Write a program or function that calculates the size of your land, given a wall you have built. You're given a non-empty input string ...
23 votes
28 answers
2k views

Count forth and back then double up

Let's count... Count up to 2 and back to 1 Count up to 4 and back to 1 Count up to 6 and back to 1 ... ok you got it... put all these together and you'll get the following sequence ...
user avatar
22 votes
20 answers
2k views

Find the "Recursive Size" of a List

Inspired by Find the “unwrapped size” of a list. Define the Recursive Size, RS, of a list containing no lists as its length (number of items contained) and the ...
22 votes
2 answers
2k views

Eye test - How many squares are in this picture?

The picture: Sick of the same old grid where the answer is simply a square pyramidal number? Accept the challenge and write a program that given a positive integer \$n\$ counts how many squares are ...
  • 2,193
22 votes
2 answers
343 views

Write a Chat Ping Counter

Your task is to write a program, that given a list of chat messages, count how many times each person gets pinged, so I can know how popular everyone is. But, since I have to do it surreptitiously, I ...
  • 25.6k
21 votes
20 answers
6k views

A penny saved is a penny

...counted! You will pass your program a variable which represents a quantity of money in dollars and/or cents and an array of coin values. Your challenge is to output the number of possible ...
21 votes
21 answers
2k views

Count the Matchsticks

Most people here are familiar with seven segment displays, which are also used in matchstick puzzles. Below are the digits 0 through ...
  • 43.1k
21 votes
3 answers
658 views

How varied is my obstacle course?

Background I have constructed a simple obstacle course by placing boxes in a rectangular room. Now I want to count the number of essentially different ways in which it can be solved. I need you to ...
  • 42.3k
21 votes
5 answers
436 views

Count cyclically self-describing lists

Cyclically self-describing lists A list \$L\$ of positive integers is cyclically self-describing, if the following conditions hold. \$L\$ is nonempty. The first and last elements of \$L\$ are ...
  • 42.3k