Posts

Showing posts from September, 2021

In Python, list of random integers

Image
def change_list(src_list): # hold the middle 6 elements of the list dest_list = src_list[3:9] # use a slice to make a new list # Print the size print('The size of the list is now', len(dest_list)) # sort the new list in ascending order dest_list.sort() return dest_list def main(): # create an empty list my_list = [] from random import randrange # use a for loop to add 12 random integers for x in range(12): # all ranging from 50 to 100 my_list.append(randrange(50, 100)) # for loop to iterate over the list and display # all elements on one line separated by a single space. print('Here is the list of random integers...', end=' ') for x in my_list: print(x, end=' ') print() # display the 4th element print('The 4th element in the list is', my_list[3]) # the element at index 9 print('The element at index 9 is', my_list[9]) # the smallest element print('The smallest eleme

In Java Swing, Prompt for cost in cents and do validations

Image
/* Display the change only if a valid price is entered (no less than 25 cents, no more than 100 cents, and an integer multiple of 5 cents). Otherwise, display separate msg messages for any of the following invalid inputs: a cost under 25 cents, a cost that is not an integer multiple of 5, and a cost that is more than a dollar. */ import javax.swing.JOptionPane; public class Test{ public static void main(String[]args){ final byte MIN = 25, MAX = 100, MOD = 5; final String prompt = String.format("Enter cost between %d and %d", MIN, MAX); String input = JOptionPane.showInputDialog(null, prompt); String msg = null; try{ int cost = Integer.parseInt(input, 10); if (MIN > cost) { msg = String.format("Cost cannot be less than %d cents", MIN); } else if (MAX < cost) { msg = String.format("Cost cannot be more than %d cents", MAX); } else if (0 != cost%MOD) { msg = String.format

In Cpp, Use vectors to compute students score

Image
#include <iostream> #include <fstream> #include <vector> #include <iomanip> using namespace std; bool read_in(vector<char> *store, char *path){ ifstream inf(path); if(inf){ char c; while(inf >> c){ store->push_back(c); } inf.close(); return true; } cout << "Error: Unable to load/read file: " << path << endl; return false; } int main(){ vector<char> answers; if(!read_in(&answers, (char*)"CorrectAnswers.txt")){ cin.get(); return 1; } vector<char> student; if(!read_in(&student, (char*)"StudentAnswers.txt")){ cin.get(); return 2; } if(answers.size() != student.size()){ cout << "Error: No. of questions & answers mismatch!" << endl; cin.get(); return 3; } int missed = 0, correct = 0; for(int i = answers.size()-1; 0 <= i; i --){ if(answers[i] != student[i]){ mi

In Javax Swing Traffic Light Simulator

Image
Download

How to load multiple language plugins in Golang?

Image
Download