Skip to content

Instantly share code, notes, and snippets.

@nonrice
Last active January 2, 2023 20:21
Show Gist options
  • Save nonrice/48db1bd985274f282c6ac1e6ba047213 to your computer and use it in GitHub Desktop.
Save nonrice/48db1bd985274f282c6ac1e6ba047213 to your computer and use it in GitHub Desktop.
Quizlet is SH!T
#include <iostream>
#include <algorithm>
#include <array>
#include <random>
#include <chrono>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
// Usage
// Make a file of cards in the form:
//
// term1:answer1
// term2:answer2
// ...
//
// Then just pass it as the first an only argument to the program:
// learner terms.txt
struct card {
std::string f, b;
};
int main(int argc, char** argv){
if (argc < 2){
std::cout << "No file provided, exiting.\n";
return 1;
}
// loading
std::vector<card*> cards;
std::ifstream fin(argv[1]);
if (!fin){
std::cout << "Invalid filename, exiting.\n";
return 1;
}
while (fin.peek() != EOF){
card* ptr = new card;
std::getline(fin, ptr->f, ':');
if (ptr->f != "\n"){
std::getline(fin, ptr->b);
cards.push_back(ptr);
}
}
fin.close();
// mechanics:
// iterate incorrect cards, shuffling between iterations
// incorrect cards remain in the deck and correct cards are deleted
int sz = cards.size();
const int ini_sz = sz;
while (sz){
int back = 0;
std::shuffle(cards.begin(), cards.begin()+sz, std::default_random_engine(std::chrono::system_clock::now().time_since_epoch().count()));
for (int i=0; i<sz; ++i){
std::cout << "\033[2JLearned: " << i-back+ini_sz-sz << '/' << ini_sz << '\n';
std::cout << cards[i]->f << '\n';
std::string ans; std::getline(std::cin, ans);
if (ans != cards[i]->b){
std::cout << "Wrong, correct answer: " << cards[i]->b << "\nOverride? (y/n)";
if (std::cin.get() == 'y') delete cards[i];
else {
cards[back] = cards[i];
++back;
}
std::cin.ignore();
} else {
delete cards[i];
}
}
sz = back;
}
std::cout << "\033[2JLearning done!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment