J

@jogomu

split

C++
3 years ago
#include <iostream> #include <numeric> #include <vector> #include <cstdlib> #include <cstring> // note: for c++20 `std::vector<std::string> &a` needs to be `std::vector<std::string> &&a` int main() { std::string pathstr="/bin:/usr/bin:/usr/local/bin";

arrayOfOdds dad idea

Java
3 years ago
public class Main{ public int[] arrayOfOdds(int highestValue) { int variable = highestValue / 2 + highestValue % 2; //if (highestValue % 2 == 0){ // variable = highestValue / 2; //} //else{ // variable = highestValue / 2 + 1; //} int [] oddArray = new int[variable];

and of N inequalities

C++
3 years ago
// this program demonstrates experimentally than when we AND together any number of inequalities (>0) // we select a single range which may be open on one end only, or closed; or we select nothing #include <iostream> #include <random> #include <functional> #include <map> #include <sstream> int main()

getters/setters

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; // this class allows setting i to anything and then fetching it class A { private int i; void set_i(int _i) {

troy parrot 000

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; /** * Week 8 Lab * Authors: Ryan White, Troy Muehlhausen */

a class protecting its function from the programmer

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; // Suppose we want to start i at 1 and only allow it to grow 1 at a time // We also want the programmer to be able to read but not set i class A { private int i; A()

show 7 bit ascii values that are printable

C++
3 years ago
#include <iostream> int main() { for(int i=0; i<128; i++) std::cout << i << (isprint(i) ? " printable":"") << std::endl; return 0; }

random ascii char with math.random in java

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main". class Main { public static void main(String[] args) { double r = Math.random(); System.out.println("Random on [0,1): " + r);

random ascii char in java

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; import java.util.concurrent.*; // The main method must be in a class named "Main". class Main { public static void main(String[] args) { //for(int i=0; i<128; i++)

Pick a random (printable) ascii character

C++
3 years ago
#include <iostream> #include <vector> constexpr bool include_non_printable = false; int main() { std::vector<int> possibilities; for(int i=0; i<128; i++) if(isprint(i) || include_non_printable)

Output to in-memory stream so that you can output to terminal stream AND/OR do something else

C++
3 years ago
#include <iostream> #include <sstream> void output_to_terminal() { std::cout << "this is a test" << std::endl; // simple output to terminal } void do_something_with_the_output(const std::string_view &sv) {

rusty ?:

Rust
3 years ago
fn main() { let x = 6; let a = if x > 5 { 10 } else { 7 }; println!("{}",a); }

?: in python

Python
3 years ago
import random random.seed() str = 'hello' if random.randint(0,1) else 'alternate reality' print (str)

you get an F

C++
3 years ago
#include <iostream> // if the parameter is outside [0,3] return -1, // else return the inversion of the list formed // by possible inputs int func(int x) { return (x<0)?-1: ((x>3)?-1: ((x==3)?0:

modulo mapping

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main". class Main { public static void main(String[] args) { System.out.println("""

won't iterate

C++
3 years ago
#include <iostream> #include <vector> int main() { std::vector<int> nums = {1,2,3}; std::cout << "the numbers:" << std::endl; for(int i=0; i<nums.size(); i++) std::cout << i << " : " << nums[i] << std::endl; std::cout << "the numbers with an extra iteration:" << std::endl; for(int i=-1; i<nums.size(); i++)

t2.cpp

C++
4 years ago
#include <string> #include <cstring> #include <cassert> #include <regex> #include <unistd.h> #include <dlfcn.h> void *build_and_dlopen(const std::string &code) { //fprintf(stderr,">>%s<<\n",code.c_str());

columnar data multi-level binary search

C++
4 years ago
#include <array> #include <cstdio> #include <cstdint> #include <cassert> #include <algorithm> int main() { std::array<int64_t,4> sample = {0,1,2,3}; // use binary search to find >=1 left->right ascending order

blowing your leg off

C++
4 years ago
#include <iostream> int main() { // behold, the power of dereferencing! int *p = (int *)0x0; printf("%d\n", *p); // causes a crash, because the pointer points to memory that we didn't allocate! return 0; // return code won't be 0 .. we never got here.. -11 means SIGSEGV (segmentation violation) }

pointers to pointers

C++
4 years ago
#include <cstdio> // printf #include <memory> // std::shared_ptr #include <string> // std::string int main() { int *p = new int(8); // regular pointer to an int on the heap (have to delete later) auto p2 = std::make_shared<int>(9); // shared pointer to another int on the heap, will auto-destruct // note: auto above could also be std::shared_ptr<int> *p2 = ... printf("%d %d\n", *p, *p2);