Skip to content

Instantly share code, notes, and snippets.

@DeclanHoare
Created August 2, 2017 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeclanHoare/d9fad519f09ad801ce9ded5d5c36c155 to your computer and use it in GitHub Desktop.
Save DeclanHoare/d9fad519f09ad801ce9ded5d5c36c155 to your computer and use it in GitHub Desktop.
shuffle stdin while removing empty lines: c++ vs python
#include <random>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
int main()
{
std::vector<std::string> files;
std::random_device rd;
std::mt19937 rnd(rd());
while(std::cin.good())
{
std::string s;
std::getline(std::cin, s);
if(!s.empty()) files.push_back(s);
}
std::shuffle(files.begin(), files.end(), rnd);
for(const auto& s: files)
std::cout << s << std::endl;
}
#!/usr/bin/env python2
import sys, random, os
lines = filter(lambda s: s != "", sys.stdin.read().splitlines())
random.shuffle(lines)
print os.linesep.join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment