Created
August 2, 2017 07:29
-
-
Save DeclanHoare/d9fad519f09ad801ce9ded5d5c36c155 to your computer and use it in GitHub Desktop.
shuffle stdin while removing empty lines: c++ vs python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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