import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.io.FileWriter;
import java.io.IOException;

class Permutation {
	/*
	 * arr[] ---> Input Array data[] ---> Temporary array to store current
	 * combination start & end ---> Staring and Ending indexes in arr[] index --->
	 * Current index in data[] r ---> Size of a combination to be printed
	 */
	static void combinationUtil(Integer[] arr, int data[], int start, int end, int index, int r,
			HashMap<String, String> UseCaseMapping, HashMap<Integer, String> UseCaseCodeNumbers,
			HashMap<String, Set<String>> ExclusionMap) {

		Set<String> combinaison = new HashSet<String>();
		String[] OutputCSV = new String[r];
		int sum = 0;

		if (index == r) {
			for (int j = 0; j < r; j++)
				sum += data[j];
			for (int j = 0; j < r; j++) {
				combinaison.add(UseCaseCodeNumbers.get(data[j]));
				OutputCSV[j] = sum + "," + UseCaseMapping.get(Integer.toString(data[j]));
			}
		// Exclusion rules to be added
			boolean Excluded = false;
			for (int j = 0; j < r; j++) {
				Excluded = false;
				if (ExclusionMap.keySet().contains(UseCaseCodeNumbers.get(data[j])))
					for (String val : ExclusionMap.get(UseCaseCodeNumbers.get(data[j]))) {
						if (combinaison.contains(val)) {
							Excluded = true;
							break;
						}
					}
				if (Excluded) {
					OutputCSV[j] += ",";
				} else
					OutputCSV[j] += "," + UseCaseCodeNumbers.get(data[j]);
				System.out.println(OutputCSV[j]);
			}
			System.out.println("------------------------------------------------");
			generateCSV(OutputCSV);

			return;
		}

		// replace index with all possible elements. The condition
		// "end-i+1 >= r-index" makes sure that including one element
		// at index will make a combination with remaining elements
		// at remaining positions
		for (int i = start; i <= end && end - i + 1 >= r - index; i++) {
			data[index] = arr[i];
			combinationUtil(arr, data, i + 1, end, index + 1, r, UseCaseMapping, UseCaseCodeNumbers, ExclusionMap);
		}
	}

	// The main function that prints all combinations of size r
	// in arr[] of size n. This function mainly uses combinationUtil()
	static void printCombination(Integer[] intarray, int n, int r, HashMap<String, String> UseCaseMapping,
			HashMap<Integer, String> UseCaseCodeNumbers, HashMap<String, Set<String>> ExclusionMap) {
		// A temporary array to store all combination one by one
		int data[] = new int[r];
		// Print all combination using temprary array 'data[]'
		combinationUtil(intarray, data, 0, n - 1, 0, r, UseCaseMapping, UseCaseCodeNumbers, ExclusionMap);
	}

	/* Driver function to check for above function */
	public static void main(String[] args) {
		HashMap<String, String> UseCaseMapping = new HashMap<String, String>();
		HashMap<Integer, String> UseCaseCodeNumbers = new HashMap<Integer, String>();
		HashMap<String, Set<String>> ExclusionMap = new HashMap<String, Set<String>>();
		String[] OutputCSV = new String[] { "Coefficient,Label,Offering Service,Year,LabelEx" };
		generateCSV(OutputCSV);

// ------------------------------------------------------------------------------------------------------------------------------------------------------------

		UseCaseMapping.put("10", "MOD01,Modernization,0"); //10 : Codenumber ; MOD01 : Use Case ; Modernization : Service Offer; 0 : Year
		UseCaseMapping.put("20", "MOD02,Modernization,0");
		UseCaseMapping.put("40", "MOD03,Modernization,0");
		//UseCaseMapping.put("81", "MOD04,Modernization,0");
		UseCaseMapping.put("100", "SP01,Service Plan,0");
		UseCaseMapping.put("200", "SP02,Service Plan,0");
		UseCaseMapping.put("400", "SP03,Service Plan,0");
		UseCaseMapping.put("800", "SP04,Service Plan,0");
		UseCaseMapping.put("1600", "SP05,Service Plan,0");
		UseCaseMapping.put("10000", "PM01,Preventive,0");
		UseCaseMapping.put("20000", "MPS01,Consulting,0");
		UseCaseMapping.put("40000", "REV01,Part Replacement,0");
		UseCaseMapping.put("100000", "EAA01,Ecostructure Asset Advisor,0");
		UseCaseMapping.put("200000", "EAA02,Ecostructure Asset Advisor,0");
		
		

		// add exclusion rules for Use Cases

		ExclusionMap.put("SP01", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02" })));//this means that SP01 is excluded by "MOD01", "MOD02"
		ExclusionMap.put("SP02", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02" })));
		ExclusionMap.put("SP04", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02" })));
		ExclusionMap.put("SP05", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02" })));
		ExclusionMap.put("SP03", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02", "SP01", "SP02", "SP03", "SP04", "SP05", "PM01", "REV01", "MPS01" })));
		ExclusionMap.put("PM01", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02", "SP01", "SP02", "SP04", "SP05" }))); //this means that SP01 is excluded by "MOD01", "MOD02", "SP01", "SP02", "SP04", "SP05"
		ExclusionMap.put("REV01", new HashSet<String>(Arrays.asList(new String[] { "MOD01", "MOD02" })));

// ------------------------------------------------------------------------------------------------------------------------------------------------------------

		for (String key : UseCaseMapping.keySet()) {
			UseCaseCodeNumbers.put(Integer.parseInt(key),
					UseCaseMapping.get(key).substring(0, UseCaseMapping.get(key).indexOf(",")));
		}

		Integer[] intarray = new Integer[UseCaseMapping.keySet().size()];
		int numberOfUC = intarray.length;
		int i = 0;
		for (String str : UseCaseMapping.keySet()) {
			intarray[i] = Integer.parseInt(str.trim());
			i++;
		}
		for (int j = 0; j <= numberOfUC; j++) {
			printCombination(intarray, numberOfUC, j, UseCaseMapping, UseCaseCodeNumbers, ExclusionMap);
		}
	}

	static void generateCSV(String[] OutputCSV) {
		String path = "C://Projects/IBGenius/Mar19 Release/MappingIBgeniusV5.csv";
		FileWriter writer;
		try {
			writer = new FileWriter(path, true);
			for (int j = 0; j < OutputCSV.length; j++) {
				writer.write(OutputCSV[j]);
				writer.write("\r\n");
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 

Javascript Online Compiler

Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.

About Javascript

Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.

Key Features

  • Open-source
  • Just-in-time compiled language
  • Embedded along with HTML and makes web pages alive
  • Originally named as LiveScript.
  • Executable in both browser and server which has Javascript engines like V8(chrome), SpiderMonkey(Firefox) etc.

Syntax help

STDIN Example

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    console.log("Hello, " + line);
});

variable declaration

KeywordDescriptionScope
varVar is used to declare variables(old way of declaring variables)Function or global scope
letlet is also used to declare variables(new way)Global or block Scope
constconst is used to declare const values. Once the value is assigned, it can not be modifiedGlobal or block Scope

Backtick Strings

Interpolation

let greetings = `Hello ${name}`

Multi line Strings

const msg = `
hello
world!
`

Arrays

An array is a collection of items or values.

Syntax:

let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);

Example:

let mobiles = ["iPhone", "Samsung", "Pixel"];

// accessing an array
console.log(mobiles[0]);

// changing an array element
mobiles[3] = "Nokia";

Arrow functions

Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.

Syntax:

() => expression

Example:

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
                                    .map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);

De-structuring

Arrays

let [firstName, lastName] = ['Foo', 'Bar']

Objects

let {firstName, lastName} = {
  firstName: 'Foo',
  lastName: 'Bar'
}

rest(...) operator

 const {
    title,
    firstName,
    lastName,
    ...rest
  } = record;

Spread(...) operator

//Object spread
const post = {
  ...options,
  type: "new"
}
//array spread
const users = [
  ...adminUsers,
  ...normalUsers
]

Functions

function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
  console.log(`Hello ${name}!`);
}
 
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar

Loops

1. If:

IF is used to execute a block of code based on a condition.

Syntax

if(condition){
    // code
}

2. If-Else:

Else part is used to execute the block of code when the condition fails.

Syntax

if(condition){
    // code
} else {
    // code
}

3. Switch:

Switch is used to replace nested If-Else statements.

Syntax

switch(condition){
    case 'value1' :
        //code
        [break;]
    case 'value2' :
        //code
        [break;]
    .......
    default :
        //code
        [break;]
}

4. For

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
//code  
} 

5. While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
  // code 
}  

6. Do-While

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
  // code 
} while (condition); 

Classes

ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.

Syntax:

class className {
  constructor() { ... } //Mandatory Class method
  method1() { ... }
  method2() { ... }
  ...
}

Example:

class Mobile {
  constructor(model) {
    this.name = model;
  }
}

mbl = new Mobile("iPhone");