/** * If the outbound level is BINARY, convert the string field to binary, then pad to the left with * the appropriate number of zero bits to reach a number of bits specified by the bitLength * attribute of the TDT definition file. */ private void binaryPadding(Map<String, String> extraparams, Field tdtfield) { String fieldname = tdtfield.getName(); int reqbitlength = tdtfield.getBitLength(); String value; String binaryValue = fieldToBinary(tdtfield, extraparams); if (binaryValue.length() < reqbitlength) { int extraBitLength = reqbitlength - binaryValue.length(); StringBuilder zeroPaddedBinaryValue = new StringBuilder(""); for (int i = 0; i < extraBitLength; i++) { zeroPaddedBinaryValue.append("0"); } zeroPaddedBinaryValue.append(binaryValue); value = zeroPaddedBinaryValue.toString(); } else { if (binaryValue.length() > reqbitlength) throw new TDTException( "Binary value [" + binaryValue + "] for field " + fieldname + " exceeds maximum allowed " + reqbitlength + " bits. Decimal value was " + extraparams.get(fieldname)); value = binaryValue; } extraparams.put(fieldname, value); }
private static String convert(List<List<Particle>> lists) { StringBuilder sb = new StringBuilder(); for (List<Particle> list : lists) { if (list.isEmpty()) sb.append('.'); else sb.append('X'); } return sb.toString(); }
public int totalNQueens(int n) { StringBuilder[] board = new StringBuilder[n]; for (int i = 0; i < n; i++) { board[i] = new StringBuilder(); for (int j = 0; j < n; j++) { board[i].append('.'); } } for (int i = 0; i < n / 2; i++) { board[0].setCharAt(i, 'Q'); dfs(n, 1, board); board[0].setCharAt(i, '.'); } ArrayList<String[]> aux = new ArrayList<String[]>(); for (String[] k : res) { String[] tmp = new String[n]; for (int i = 0; i < n; i++) { StringBuilder sb = new StringBuilder(k[i]).reverse(); tmp[i] = sb.toString(); } aux.add(tmp); } res.addAll(aux); if (n % 2 != 0) { board[0].setCharAt(n / 2, 'Q'); dfs(n, 1, board); board[0].setCharAt(n / 2, '.'); } return res.size(); }
/** * Return a set of the variables in the supplied expression. Note: Substitutions which are in the * constant table are not included. */ public Set<String> getVariablesWithin(String exp) { Set<String> all = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); String add = null; if (separators == null) { StringBuilder sep = new StringBuilder(10); for (char chr = 0; chr < operators.length; chr++) { if (operators[chr] != null && !operators[chr].internal) { sep.append(chr); } } sep.append("()"); separators = sep.toString(); } for (StringTokenizer tkz = new StringTokenizer(exp, separators, true); tkz.hasMoreTokens(); ) { String tkn = tkz.nextToken().trim(); if (tkn.length() != 0 && Character.isLetter(tkn.charAt(0))) { add = tkn; } else if (tkn.length() == 1 && tkn.charAt(0) == '(') { add = null; } else if (add != null && !constants.containsKey(add)) { all.add(add); } } if (add != null && !constants.containsKey(add)) { all.add(add); } return all; }
public static String set(final String lhs, BigInteger index, char value) { int idx = index.intValue(); // hmmm, not exactly efficient! StringBuilder sb = new StringBuilder(lhs); sb.setCharAt(idx, value); return sb.toString(); }
/** * Returns a string representation of all found arguments. * * @param args array with arguments * @return string representation */ static String foundArgs(final Value[] args) { // compose found arguments final StringBuilder sb = new StringBuilder(); for (final Value v : args) { if (sb.length() != 0) sb.append(", "); sb.append(v instanceof Jav ? Util.className(((Jav) v).toJava()) : v.seqType()); } return sb.toString(); }
public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); }
/** * Converts a binary string input into an ASCII string output, assuming that 7-bit compaction was * used */ private String bin2asciiseven(String binary) { String asciiseven; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 7) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 7), 8))); buffer.append((char) j); } asciiseven = buffer.toString(); return asciiseven; }
/** * Converts an ASCII string input into a binary string, using 7-bit compaction of each ASCII byte */ private String asciiseven2bin(String asciiseven) { String binary; StringBuilder buffer = new StringBuilder(""); int len = asciiseven.length(); byte[] bytes = asciiseven.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 128)), 8).substring(1, 8)); } binary = buffer.toString(); return binary; }
/** Converts a byte string input into a binary string, using 8-bits per character byte */ private String bin2bytestring(String binary) { String bytestring; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 8) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 8), 8))); buffer.append((char) j); } bytestring = buffer.toString(); return bytestring; }
/** Converts a binary string input into a byte string, using 8-bits per character byte */ private String bytestring2bin(String bytestring) { String binary; StringBuilder buffer = new StringBuilder(""); int len = bytestring.length(); byte[] bytes = bytestring.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i])), 8)); } binary = buffer.toString(); return binary; }
/** * Converts a binary string input into a character string output, assuming that 5-bit compaction * was used */ private String bin2uppercasefive(String binary) { String uppercasefive; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 5) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 5), 8))); buffer.append((char) (j + 64)); } uppercasefive = buffer.toString(); return uppercasefive; }
/** * Converts an upper case character string input into a binary string, using 5-bit compaction of * each ASCII byte */ private String uppercasefive2bin(String uppercasefive) { String binary; StringBuilder buffer = new StringBuilder(""); int len = uppercasefive.length(); byte[] bytes = uppercasefive.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 32)), 8).substring(3, 8)); } binary = buffer.toString(); return binary; }
/** * Converts an alphanumeric string input into a binary string, using 6-bit compaction of each * ASCII byte */ private String alphanumsix2bin(String alphanumsix) { String binary; StringBuilder buffer = new StringBuilder(""); int len = alphanumsix.length(); byte[] bytes = alphanumsix.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 64)), 8).substring(2, 8)); } binary = buffer.toString(); return binary; }
private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); }
private Number readNumber() { char c; boolean d = false; StringBuilder sb = new StringBuilder(); while (true) switch (c = peek()) { case '.': case 'e': case 'E': case '+': d = true; case '-': sb.append(next()); continue; default: if (check(c, '0', '9')) sb.append(next()); else if (check(c, ",:; \n\r\t>}])")) { return d ? new BigDecimal(sb.toString()) : new BigInteger(sb.toString()); } else throw new RuntimeException("unexpected character: " + c); } }
/** * Pads a binary value supplied as a string first parameter to the left with leading zeros in * order to reach a required number of bits, as expressed by the second parameter, reqlen. Returns * a string value corresponding to the binary value left padded to the required number of bits. */ private String padBinary(String binary, int reqlen) { String rv; int l = binary.length(); int pad = (reqlen - (l % reqlen)) % reqlen; StringBuilder buffer = new StringBuilder(""); for (int i = 0; i < pad; i++) { buffer.append("0"); } buffer.append(binary); rv = buffer.toString(); return rv; }
/** * Make a string from the contents of this JSONArray. The <code>separator</code> string is * inserted between each element. Warning: This method assumes that the data structure is * acyclical. * * @param separator A string that will be inserted between the elements. * @return a string. * @throws JSONException If the array contains an invalid number. */ public String join(String separator) throws JSONException { int len = this.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(this.myArrayList.get(i))); } return sb.toString(); }
/** * Returns a string built using a particular grammar. Single-quotes strings are counted as literal * strings, whereas all other strings appearing in the grammar require substitution with the * corresponding value from the extraparams hashmap. */ private String buildGrammar(String grammar, Map<String, String> extraparams) { StringBuilder outboundstring = new StringBuilder(); String[] fields = Pattern.compile("\\s+").split(grammar); for (int i = 0; i < fields.length; i++) { if (fields[i].substring(0, 1).equals("'")) { outboundstring.append(fields[i].substring(1, fields[i].length() - 1)); } else { outboundstring.append(extraparams.get(fields[i])); } } return outboundstring.toString(); }
/** * Converts a binary string input into a character string output, assuming that 6-bit compaction * was used */ private String bin2alphanumsix(String binary) { String alphanumsix; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 6) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 6), 8))); if (j < 32) { j += 64; } buffer.append((char) j); } alphanumsix = buffer.toString(); return alphanumsix; }
// Find and unescape a string. private String readString() { StringBuilder sb = new StringBuilder(); char c = next(); if (c != '"') throw new RuntimeException("expecting start of string"); while (true) switch (c = next()) { case '"': return sb.toString(); case '\\': sb.append(readEscaped()); continue; default: if (Character.isISOControl(c)) throw new RuntimeException("illegal character in string"); sb.append(c); } }
public String readword() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') { c = in.read(); } if (c < 0) { return ""; } while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); }
/** pad a value according the field definition. */ private void padField(Map<String, String> extraparams, Field field) { String name = field.getName(); String value = extraparams.get(name); PadDirectionList padDir = field.getPadDir(); int requiredLength = field.getLength(); // assert value != null; if (value == null) return; String padCharString = field.getPadChar(); // if no pad char specified, don't attempt padding if (padCharString == null) return; assert padCharString.length() > 0; char padChar = padCharString.charAt(0); StringBuilder buf = new StringBuilder(requiredLength); if (padDir == PadDirectionList.LEFT) { for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar); buf.append(value); } else if (padDir == PadDirectionList.RIGHT) { buf.append(value); for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar); } assert buf.length() == requiredLength; if (requiredLength != value.length()) { // System.out.println(" updated " + name + " to '" + buf + "'"); extraparams.put(name, buf.toString()); } /* else { StringBuilder mybuf = new StringBuilder(); for (int i = 0; i < value.length(); i++) { if (i > 0) mybuf.append(','); mybuf.append('\''); mybuf.append(value.charAt(i)); mybuf.append('\''); } System.out.println(" field " + name + " not padded as " + mybuf.toString() + " is already " + requiredLength + " characters long"); } */ }
public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); int l = s.length(); Double sqrl = Math.sqrt(l); Double low = Math.floor(sqrl); Double high = Math.ceil(sqrl); int r = 0; int c = 0; if (low == high) { r = low.intValue(); c = low.intValue(); } else if (low * high >= l) { r = low.intValue(); c = high.intValue(); } else { r = high.intValue(); c = high.intValue(); } // System.out.println("Row: " + r); // System.out.println("Col: " + c); StringBuilder sb = new StringBuilder(); char en[] = s.toCharArray(); for (int i = 0; i < c; i++) { for (int j = 0; j < r; j++) { if (j * c + i < l) { sb.append(en[j * c + i]); } } sb.append(" "); } System.out.println(sb.toString()); }
// Try to read an atom. private Atom readAtom() { StringBuilder sb = new StringBuilder(); for (char c = peek(); validAtomPart(c); c = peek()) sb.append(next()); return new Atom(sb.toString()); }
/* ------------------------------------------------------------------------------------------------------- This method: This method: -- Takes in three paramters: 1. array - this is the byte array that actually holds the document contents 2. md5Hases - holds the entire hash values of the document 3. Divisor1/Divisor2/divisor3... - main and back up divisors 5. The remainder we are looking for 6/7. min/max boundaries -- We will start running the karb rabin algorithm -- We will find the boundaries using mod values and once they equal the mod value we have stored -- we also have the divsor2/3 .. which are backup divisors. If we don't find a boundary by the divisor1 once we hit the maxBoundary -- we will see if we have one with divisor2, if not, then we will see if we have one with divisor3 and so on -- We will hash everything in that hash boundary and store it -------------------------------------------------------------------------------------------------------- */ private static void runTddd( byte[] array, ArrayList<Long> md5Hashes, long divisor1, long divisor2, long divisor3, long remainder, long minBoundary, long maxBoundary) { int documentStart = 0; // used to keep track of where the boundaries are boolean match = false; // used to ck if we encountered a match int backUpBreakPoint = -1; // used to store the backup breakpoint int secondBackUpBreakPoint = -1; // used with the divisor3 StringBuilder builder = new StringBuilder(); int i = documentStart + (int) minBoundary - 1; // so we start at the minimum // loop through all the values in the document for (; i < md5Hashes.size() - 1; ++i) { // if ((i - documentStart + 1) < minBoundary) // if the size of this boundary is less than // the min, continue looping // continue; /*----------------------------------------------------------------- - If the mod of this equals the modvalue we defined, then - this is a boundary ------------------------------------------------------------------*/ if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor1 == remainder) // ck if this equals the mod value { // Hash all the values in the range (documentStart,current(i)) // Remember we only want to hash the original VALUES from the array that contains the // original // content of the file. Not the hash values in the md5Hash Array for (int j = documentStart; j <= i; ++j) { builder.append(array[j]); // store everything upto the current value } String original = builder.toString(); // if the string is a perfect match ( hash and original string) if (HashClass.is_string_match(original, table)) // iinsert the hash in the table) coverage += i - documentStart + 1; // this is the amount of bytes we saved documentStart = i + 1; // set this as the beginning of the new boundary backUpBreakPoint = -1; // reset this secondBackUpBreakPoint = -1; numOfPieces++; // increment the num of pieces i = i + (int) minBoundary - 1; // skip all the way here builder.setLength(0); // reset the stringBuilder for the next round } else if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor2 == remainder) { // check if this is the backup point backUpBreakPoint = i; // this is the backup breakpoint } else if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor2 == remainder) { secondBackUpBreakPoint = i; // set the second backup point } if ((i - documentStart + 1) >= maxBoundary) { // we have reached the maximum // ck if we have a backUpbreakpoint int point; if (backUpBreakPoint != -1) // if we do, set this as the boundary point = backUpBreakPoint; else if (secondBackUpBreakPoint != -1) { point = secondBackUpBreakPoint; // if we don't have a first break point, find the second } else point = i; // else this current value of i is the breakpoint // Hash all the values in the range (documentStart,current(i)) // Remember we only want to hash the original VALUES from the array that contains the // original // content of the file. Not the hash values in the md5Hash Array for (int j = documentStart; j <= point; ++j) { builder.append(array[j]); // store everything upto the current value } String original = builder.toString(); if (HashClass.is_string_match(original, table)) coverage += point - documentStart + 1; // this is the amount of bytes we saved numOfPieces++; // increment the num of pieces documentStart = point + 1; // set this as the beginning of the new boundary backUpBreakPoint = -1; // reset this secondBackUpBreakPoint = -1; // reset the secondBackUp point i = point + (int) minBoundary - 1; // skip all the way here ; builder.setLength(0); // reset the stringBuilder for the next round } } // end of the for loop // ------------------------------------------------------------------------------------------- // we are missing the last boundary, so hash that last value // We will also check against our values of the strings we already have, and if we encountered // this // already, then we will simply increment the counter, otherwise we will insert it in the // hashtable // and increase our miss counter // ---------------------------------------------------------------------------------------------- for (int j = documentStart; j < array.length; ++j) { builder.append(array[j]); } // only compute hash and insert into our hashtable only if the string buffer isn't empty String original = builder.toString(); if (HashClass.is_string_match(original, table)) coverage += array.length - documentStart; // this is the amount of bytes we saved numOfPieces++; // increment the num of pieces } // end of the method
/** * Adds additional entries to the extraparams hashmap by processing various rules defined in the * TDT definition files. Typically used for string processing functions, lookup in tables, * calculation of check digits etc. */ private void processRules(Map<String, String> extraparams, Rule tdtrule) { String tdtfunction = tdtrule.getFunction(); int openbracket = tdtfunction.indexOf("("); assert openbracket != -1; String params = tdtfunction.substring(openbracket + 1, tdtfunction.length() - 1); String rulename = tdtfunction.substring(0, openbracket); String[] parameter = params.split(","); String newfieldname = tdtrule.getNewFieldName(); // System.out.println(tdtfunction + " " + parameter[0] + " " + extraparams.get(parameter[0])); /** * Stores in the hashmap extraparams the value obtained from a lookup in a specified XML table. * * <p>The first parameter is the given value already known. This is denoted as $1 in the * corresponding XPath expression * * <p>The second parameter is the string filename of the table which must be present in the * auxiliary subdirectory * * <p>The third parameter is the column in which the supplied input value should be sought * * <p>The fourth parameter is the column whose value should be read for the corresponding row, * in order to obtain the result of the lookup. * * <p>The rule in the definition file may contain an XPath expression and a URL where the table * may be obtained. */ if (rulename.equals("TABLELOOKUP")) { // parameter[0] is given value // parameter[1] is table // parameter[2] is input column supplied // parameter[3] is output column required assert parameter.length == 4 : "incorrect number of parameters to tablelookup " + params; if (parameter[1].equals("tdt64bitcpi")) { String s = extraparams.get(parameter[0]); assert s != null : tdtfunction + " when " + parameter[0] + " is null"; String t = gs1cpi.get(s); assert t != null : "gs1cpi[" + s + "] is null"; assert newfieldname != null; extraparams.put(newfieldname, t); // extraparams.put(newfieldname, gs1cpi.get(extraparams.get(parameter[0]))); } else { // JPB! the following is untested String tdtxpath = tdtrule.getTableXPath(); String tdttableurl = tdtrule.getTableURL(); String tdtxpathsub = tdtxpath.replaceAll("\\$1", extraparams.get(parameter[0])); extraparams.put(newfieldname, xpathlookup("ManagerTranslation.xml", tdtxpathsub)); } } /** * Stores the length of the specified string under the new fieldname specified by the * corresponding rule of the definition file. */ if (rulename.equals("LENGTH")) { assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (extraparams.get(parameter[0]) != null) { extraparams.put(newfieldname, Integer.toString(extraparams.get(parameter[0]).length())); } } /** * Stores a GS1 check digit in the extraparams hashmap, keyed under the new fieldname specified * by the corresponding rule of the definition file. */ if (rulename.equals("GS1CHECKSUM")) { assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (extraparams.get(parameter[0]) != null) { extraparams.put(newfieldname, gs1checksum(extraparams.get(parameter[0]))); } } /** * Obtains a substring of the string provided as the first parameter. If only a single second * parameter is specified, then this is considered as the start index and all characters from * the start index onwards are stored in the extraparams hashmap under the key named * 'newfieldname' in the corresponding rule of the definition file. If a second and third * parameter are specified, then the second parameter is the start index and the third is the * length of characters required. A substring consisting characters from the start index up to * the required length of characters is stored in the extraparams hashmap, keyed under the new * fieldname specified by the corresponding rule of the defintion file. */ if (rulename.equals("SUBSTR")) { assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (parameter.length == 2) { if (extraparams.get(parameter[0]) != null) { int start = getIntValue(parameter[1], extraparams); if (start >= 0) { extraparams.put(newfieldname, extraparams.get(parameter[0]).substring(start)); } } } if (parameter.length == 3) { // need to check that this variation is correct - c.f. Perl substr assert extraparams.get(parameter[0]) != null : tdtfunction + " when " + parameter[0] + " is null"; if (extraparams.get(parameter[0]) != null) { int start = getIntValue(parameter[1], extraparams); int end = getIntValue(parameter[2], extraparams); if ((start >= 0) && (end >= 0)) { extraparams.put( newfieldname, extraparams.get(parameter[0]).substring(start, start + end)); } } } } /** * Concatenates specified string parameters together. Literal values must be enclosed within * single or double quotes or consist of unquoted digits. Other unquoted strings are considered * as fieldnames and the corresponding value from the extraparams hashmap are inserted. The * result of the concatenation (and substitution) of the strings is stored as a new entry in the * extraparams hashmap, keyed under the new fieldname specified by the rule. */ if (rulename.equals("CONCAT")) { StringBuilder buffer = new StringBuilder(); for (int p1 = 0; p1 < parameter.length; p1++) { Matcher matcher = Pattern.compile("\"(.*?)\"|'(.*?)'|[0-9]").matcher(parameter[p1]); if (matcher.matches()) { buffer.append(parameter[p1]); } else { assert extraparams.get(parameter[p1]) != null : tdtfunction + " when " + parameter[p1] + " is null"; if (extraparams.get(parameter[p1]) != null) { buffer.append(extraparams.get(parameter[p1])); } } } extraparams.put(newfieldname, buffer.toString()); } }