Parsing Arrays in C++An array can be defined as a data structure consisting of a group of elements. Basically, all these elements have the same data type, for example, a string or integer. Arrays are used in C++ and other programming languages to organize data or information, so that, values and var...
Hash tables in Java
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class SpellChecker {
// Static collection for lowercase alphabet sybols
private static final List alphabetic;
static {
alphabetic = new ArrayList<>();
for (char c = 'a'; c<= 'z'; c++) {
alphabetic.add(c);
}
}
// Map, containing seof misspellings for any word in dictionary
private final Map> words;
/**
* SpellChecker constructor from given dictionary file
* @param filename dictionary file
* @throws IOException thrown, on file reading
*/
public SpellChecker(String filename) throws IOException {
// create file scanner and reading line by line
try(Scanner scanner = new Scanner(new File(filename))) {
// create map
words = new HashMap<>();
while (scanner.hasNextLine()) {
// read line and put word's fuzzies to the map
String word = scanner.nextLine().trim();
words.put(word, getFuzzy(word));
}
}
}
/**
* Method, providing possible correct version of misspelled word
* @param word misspelled word
* @return collection of possible dictionary words
*/
public CollectiongetSuggest(String word) {
// if word matches any word in dictionary, return singleton collection
if (words.containsKey(word))
return Collections.singleton(word);
// Iterating over all words in dictionary, and collect any word, which contains current as a fuzzy
Collection result = new ArrayList<>();
for(Map.Entry> pair : words.entrySet()) {
if (pair.getValue().contains(word)) {
result.add(pair.getKey());
}
}
return result;
}
/**
* Generates collections of fuzzies for given word
* @param word
* @return
*/
private CollectiongetFuzzy(String word) {
Collection fuzzies = new ArrayList<>();
// missing symbol fuzzy
fuzzies.addAll( IntStream.range(0, word.length())
.mapToObj(i ->word.substring(0,i) + word.substring(i+1))
.collect(Collectors.toList()));
// wrong symbol fuzzy
fuzzies.addAll( IntStream.range(0, word.length())
.boxed()
.flatMap(i ->alphabetic.stream()
.map(c ->word.substring(0, i) + c + word.substring(i + 1)))
.collect(Collectors.toList()));
// inserted symbol fuzzy
fuzzies.addAll( IntStream.range(0, word.length() + 1)
.boxed()
.flatMap(i ->alphabetic.stream()
.map(c ->word.substring(0, i) + c + word.substring(i)))
.collect(Collectors.toList()));
// transposition fuzzy
fuzzies.addAll( IntStream.range(0, word.length() - 1)
.boxed()
.flatMap(i ->alphabetic.stream()
.map(c ->word.substring(0, i) + word.charAt(i+1) + word.charAt(i) + word.substring(i+2)))
.collect(Collectors.toList()));
return fuzzies;
}
public static void main(String[] args) throws IOException {
// create spellChecker object
SpellCheckersc = new SpellChecker("dictionary.txt");
// test word in lowercas
String word = "erlevant";
word = word.toLowerCase();
// output possible choices.
System.out.println(String.join(", ", sc.getSuggest(word)));
}
}
Java classes and objects
import java.util.Scanner;
public class ChocolateCoupons {
// Entry point of the program
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Ask the money of the user
System.out.println("How much money can you spend on chocolate bars?");
int cash = in.nextInt();
// Calculate how many chocolate bars can be bought
// Each choc costs $1
int numChocs = cash;
// Each choc has a coupon
int numCoupons = numChocs;
// We use the coupons to buy another set of chos
// 6 coupons is to 1 choc
while (numCoupons>= 6) {
numCoupons -= 6;
numChocs += 1;
// More coupon for each choc
numCoupons++;
}
// Report the total number of chocs
System.out.println("After redeeming coupons, you have "
+ numCoupons + " leftover coupons and can purchase a total of "
+ numChocs + " chocolate bars.");
}
}