Problem:
You encounter a GCC error message of the form
error: default argument for parameter of type <some type> has type <some other type>
You encounter a GCC error message of the form
error: default argument for parameter of type <some type> has type <some other type>
In C++ you want to check if a given file exists, but you can’t use stat()
because your code needs to work cross-plaform.
You have installed zsh but you don’t see the menu-style autocompletion (where you can navigate the suggestions using the arrow keys on the keyboard)
The following small ANSI C99 program reads a String from stdin and prints out a Brainfuck program that prints the same String on stdout.
Compile using gcc -o bf bf.c
Use it like this:
cat my.txt | ./bf > my.bf
Source code:
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { unsigned char c; unsigned char curval = 0; //Initialize reg+1 with 8 while(1) { c = getchar(); if(feof(stdin)) {break;} while(curval != c) { if(curval < c) { putchar('+'); curval++; } else if(curval > c) { putchar('-'); curval--; } } putchar('.'); } }
How does it work?
Basically it uses just one of the registers of the Brainfuck Turing machine and incremets or decrements the register to be able to print out the next byte. It doesn’t use any of the more ‘advanced’ features in Brainfuck like loops.
In the Linux shell, you have a file path and you want to strip everything but the filename, for example you have the path ../images/photo.jpg
and want only photo.jpg
You’ve got a big problem. Someone forces encourages you to implement a complete Othello UI+AI in Java, but you don’t have any idea how to do that. If you already know how to implement the basics and you are interested in more advanced strategy concepts, you might be interested in the other parts of this series (yet to come).
In this multi-part series I will not provide any complete solution to any of the standard Othello tasks. Instead, I will provide (hopefully) helpful hints how to get your coding going and explain how your code works.
You have ZIP archive and want to convert it to TAR automatically conserving the original directory structure.
You have a filename in the Linux shell and want to strip/remove the filename extension from it – e.g. if you have myarchive.zip
, you want to get only myarchive
as output.
In C/C++ you want to encode/decode something from/to Base64. libtomcrypt is WTFPL-licensed and therefore provides a good choice for both commercial and non-commercial projects.
You want to calculate a hash of any string in C/C++. LibTomCrypt is WTFPL licensed, so it’s a good choice for commercial and non-commercial projects.
In GNU R, you have an arbitrary string s and want to count how often a given character occurs in s.
You want to use a custom column type for your LaTeX tabular that has a specific font attribute (we’ll use the font family for this example).
You’re using Maven as build system but you need to include your Java sources into your JAR file generated by mvn package
.
In many cases if you want to create an interactive command line interface, you need to check if a number entered by the user is valid and – if it isn’t – you want the user to re-input it.
Here’s a simple static method to check if a number typed by a user is within a given boundary:
/**
* Lets the user input an integer value until it satisfies the given
* conditions
*
* @param msg The prompt to ask the user for the value.
* @param lower The lower boundary, inclusive
* @param upper The upper boundary, inclusive
*/
private static int guardedInput(String msg, int lower, int upper) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(msg);
int val = -1;
try {
val = scanner.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Illegal value: Please type a number!");
}
if (val < lower) {
System.out.println("Illegal value: Must be greater than " + (lower - 1));
} else if (val > upper) {
System.out.println("Illegal value: Must be smaller than " + (upper + 1));
} else {
return val;
}
}
}
When running any npm
command, you get a stacktrace similar to the following:
Error: Cannot find module 'graceful-fs' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at Object.<anonymous> (/usr/share/npm/lib/utils/ini.js:32:10) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:362:17)
When executing node-waf
, you get the following error message:
Traceback (most recent call last): File "/usr/local/bin/node-waf", line 14, in <module> import Scripting ImportError: No module named Scripting
This also prevents node modules with native components to be built.
In C++11 you want to iterate over a smart pointer (auto_ptr
, shared_ptr
, …). collection, say a std::vector
, using the new for
loop syntax.
Let’s try it out:
using namespace std; shared_ptr<vector<int> > smartptr(/* A ptr to your vector */); for(int s : smartptr) { /* do something useful */ }
When trying to compile this code, GCC emits the following error message (other lines are omitted for the sake of simplicity)
error: no matching function for call to 'begin(std::shared_ptr<std::vector<int> >&)' error: no matching function for call to 'end(std::shared_ptr<std::vector<int> >&)'
or, when LANG=de
is set:
Fehler: keine passende Funktion für Aufruf von »begin(std::shared_ptr<std::vector<int> >&)« Fehler: keine passende Funktion für Aufruf von »end(std::shared_ptr<std::vector<int> >&)«
In NodeJS, you got a size of a file in bytes, but you want to format it for better readability.
For example, if your size is 10000
bytes, you want to print 10 kilobytes
, but if it is 1200000
, you want to print 1.20 Megabytes
.
To determine the size of a file in NodeJS (e.g. to get the size of myfile.txt
) use fs.stat()
or fs.statSync()
like this:
const fs = require("fs"); //Load the filesystem module const stats = fs.statSync("myfile.txt"); const fileSizeInBytes = stats.size; //Convert the file size to megabytes (optional) const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
Another option is to use the following function:
function getFilesizeInBytes(filename) { const stats = fs.statSync(filename); const fileSizeInBytes = stats.size; return fileSizeInBytes; }