Posts

Showing posts from April, 2021

In Excel VBA, Reading Local Disk File

Image
Download

In Excel VBA, Fetch Data From HTTP URL

Image
Download

In Excel VBA, Parse JSON String To Dictionary

Image
Download

In Python, Scramble String And Check If Two Strings Are Anagram

Image
from random import shuffle def scramble_letters(txt): chars = list(txt) shuffle(chars) return ''.join(chars) def is_anagram(s1, s2): return ''.join(sorted(list(s1))) == ''.join(sorted(list(s2)))

In C, Compute Total Retail Price Of Products Sold

Image
Download #include<stdio.h> void main(){ const float prices[]={2.95f, 4.99f, 5.49f, 7.80f, 8.85f}; const int len = sizeof(prices)/sizeof(float); float total = 0.0f; int no, i, qty; do{ printf("Product No. Price\n"); printf("------------------\n"); for(i = 0; len > i; i ++) printf("%d)%10s$%.2f\n", (1+i), "", prices[i]); fflush(stdin); printf("\nEnter product no# (0 to quit): "); if(!scanf("%d", &no)){ printf("Error: Invalid input!\n"); continue; } if(0 == no) break; if(1 > no || len <= no){ printf("Error: Invalid product no#!\n"); continue; } fflush(stdin); printf("\nEnter quantity: "); if(!scanf("%d", &qty)){ printf("Error: Invalid input!\n"); co

In Java, Project Donation For Cheery Charities

Image
import java.util.Scanner; class Project{ private double targetDonation; private double actualDonation; private String name; private String sponsor; private int nosOfDonations; public Project(){ this(1000, "", ""); } public Project(double targetDonation, String name){ this(targetDonation, name, ""); } public Project(double targetDonation, String name, String sponsor){ setTargetDonation(targetDonation); setSponsor(sponsor); setName(name); setNosOfDonations(0); } public boolean setTargetDonation(double targetDonation){ if(0 < targetDonation){ this.targetDonation = targetDonation; return true; } return false; } public boolean addDonation(double donation){ boolean added = setActualDonation(getActualDonation() + (donation*2)); if(added) nosOfDonations ++; return added; }

In Java, Check For Identical Arrays

Image
import java.util.Scanner; public class IdenticalArrays{ public static boolean equals(int[][] m1, int[][] m2){ if(m1 == m2) return true; boolean same = false; if(m1.length == m2.length){ same = true; for(int i = 0,j; same && m1.length > i; i ++){ if(m1[i].length == m2[i].length){ for(j = 0; same && m1[i].length > j; j ++){ if(m1[i][j] != m2[i][j]){ same = false; break; } } } else { same = false; break; } } } return same; } private static int[][] read(String prompt, Scanner in, final int r, final int c){ int[][]arr = new int[r][c]; System.out.print(prompt); for(int i = 0,j; r > i; i ++) for(j = 0;

In Java, Write And Read 100 Random Integers From File

Image
import java.io.PrintStream; public class Ex3{ public static void main(String[]args){ try(PrintStream out = new PrintStream(new java.io.File("numbers.txt"))){ int i = 100; while(0 < i --) out.println((int)(1+Math.random()*10000)); }catch(java.io.IOException e){ System.err.println(e.getMessage()); } } } Download

In Java, Analyze Scores Given By User

Image
import java.util.Scanner; public class AnalyzeScores{ public static void main(String[]args){ int num, sum = 0, cnt = 0; int[]nums=new int[100]; Scanner in = new Scanner(System.in); System.out.print("Enter N numbers (-1 to quit): "); while(in.hasNextInt() && cnt < nums.length){ num = in.nextInt(); if(0 > num) break; nums[cnt++] = num; sum += num; } if(0 == cnt){ System.out.println("Error: No inputs given!"); return; } int avg = sum/cnt, abAvg = 0, blAvg = 0; for(int i = cnt-1; 0 <= i; i --) if(nums[i] >= avg) abAvg ++; else blAvg ++; System.out.printf("Nos# above avg: %d\n", abAvg); System.out.printf("Nos# below avg: %d\n", blAvg); } }

In Java, Find Distinct Numbers From Array Of Integers Given By User

Image
import java.util.Scanner; public class DistinctNumbers{ public static void main(String[]args){ int num, cnt = 0, i, j = 0; int[]nums=new int[10]; Scanner in = new Scanner(System.in); System.out.print("Enter ten numbers: "); while(cnt < nums.length && in.hasNextInt()){ num = in.nextInt(); cnt ++; i = j; while(0 < i-- && nums[i] != num); if(-1 == i) nums[j++] = num; } if(0 == j){ System.out.println("Error: No inputs given!"); return; } System.out.println("The number of distinct numbers is "+j); System.out.print("The distinct numbers are:"); i = 0; while(i < j) System.out.print(" "+nums[i++]); } }