15 ianuarie 2019
22 august 2017
Property value reader example / Java
# Settings for starting Matlab in stay-alive mode
matlab_path=C:/Users/Me/Desktop/backend
matlab_exe_filename=lev_lis_interface_main.exe
matlab_exe_params=standard log.txt logs
-------------
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by Me on 8/22/2017. */public class MatlabPropertyValues { private static final Logger LOGGER = LoggerFactory.getLogger(MatlabPropertyValues.class); private String executablePath; private String executableName; private String executableParams; InputStream inputStream = null; private static MatlabPropertyValues instance = null; public static MatlabPropertyValues getInstance() { if (instance == null) { instance = new MatlabPropertyValues(); instance.loadPropValues(); } return instance; } private void loadPropValues() { try { Properties prop = new Properties(); String propFileName = "config.properties"; inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); // may be different if (inputStream != null) { prop.load(inputStream); } else { throw new FileNotFoundException("Property file '" + propFileName + "' not found in the classpath"); } executablePath = prop.getProperty("matlab_path"); executableName = prop.getProperty("matlab_exe_filename"); executableParams = prop.getProperty("matlab_exe_params"); } catch (Exception e) { e.printStackTrace(); LOGGER.error(e.getMessage()); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); LOGGER.error(e.getMessage()); } } } public String getExecutablePath() { return executablePath; } public String getExecutableName() { return executableName; } public String getExecutableParams() { return executableParams; } public static List<String> getPropertyValuesAsList() { List<String> args = new ArrayList<>(); MatlabPropertyValues instance = getInstance(); args.add(instance.getExecutablePath()); args.add(instance.getExecutablePath() + "\\" + instance.getExecutableName()); String params [] = instance.getExecutableParams().split(" "); args.add(params[0] + " " + (instance.getExecutablePath() + "\\" + params[1]) + " " + (instance.getExecutablePath() + "\\" + params[2])); return args; } }
08 martie 2017
Generate classes for XML Schema (xjc)
Fie o schema (model.xsd):
Si o implementare (input.xml):
Metoda (1)
In Maven (pom.xml)
(mvn clean install) => target/generatedSources/jaxb/generated/
Metoda (2)
Cu xjc din suita jdk-ului:
> xjc -d <path-to-folder-to-add> -p <package-name> model.xsd
Comanda creeaza un proiect nou avand fisierele sursa generate din schema.
Utilizare pentru parsarea lui input.xml:
Si o implementare (input.xml):
Metoda (1)
In Maven (pom.xml)
(mvn clean install) => target/generatedSources/jaxb/generated/
Metoda (2)
Cu xjc din suita jdk-ului:
> xjc -d <path-to-folder-to-add> -p <package-name> model.xsd
Comanda creeaza un proiect nou avand fisierele sursa generate din schema.
Utilizare pentru parsarea lui input.xml:
06 martie 2017
Code jam, for women (solutiile mele, #2)
Which two dancers will be next to dancer number K when the dance is over?
# init
T = 0
i = 1
def solve(caseId, D, K, N):
pos = [0 for i in range(D)]
i = 1
while i<= D:
# even -> ccw
if i % 2 == 0:
pos[(i - N) % D] = i
# odd -> cw
else:
pos[(i + N) % D] = i
i += 1
posK = pos.index(K)
msg = "Case #"+ str(caseId) + ": " + str(pos[(posK+1)%D]) + " " + str(pos[(posK-1)%D])
print msg
with open("B-large-practice.in", "r") as myFile:
T = eval(myFile.readline())
for line in myFile:
D, K, N = line.split(' ')
solve (i, eval(D), eval(K), eval(N))
i = i + 1
myFile.close()
# This solution does not scale !!!
# if D is very large, we can't afford to use an array that large!
# hence got this math solution below:
case = 1
def fix(number, D):
while number <= 0:
number += D
while number > D:
number -= D
return number
def getLeft(index_K, D, N):
if index_K == D-1:
index_left = 0
else:
index_left = index_K + 1
j = 0 # does not matter as we add/subtract as we want
left = D*j + index_left + N
if left % 2 == 0:
return fix(left,D)
left = D*j + index_left - N
if left % 2 == 1:
return fix(left,D)
return -1
def getRight(index_K, D, N):
if index_K == 0:
index_right = D-1
else:
index_right = index_K - 1
j = 0
right = D*j + index_right + N
if right % 2 == 0:
return fix(right,D)
right = D*j + index_right - N
if right % 2 == 1:
return fix(right,D)
return -1
def solve(caseId, D, K, N):
if K % 2 == 0:
index_K = (K - N) % D # K is at position (K-N)%D
left = getLeft (index_K, D, N)
right = getRight (index_K, D, N)
else:
index_K = (K + N) % D
left = getLeft (index_K, D, N)
right = getRight (index_K, D, N)
msg = "Case #"+ str(caseId) + ": " + str(left) + " " + str(right)
print msg
with open("B-large-practice.in", "r") as myFile:
T = eval(myFile.readline())
for line in myFile:
D, K, N = line.split(' ')
solve (case, eval(D), eval(K), eval(N))
case = case + 1
myFile.close()
# hence got this math solution below:
case = 1
def fix(number, D):
while number <= 0:
number += D
while number > D:
number -= D
return number
def getLeft(index_K, D, N):
if index_K == D-1:
index_left = 0
else:
index_left = index_K + 1
j = 0 # does not matter as we add/subtract as we want
left = D*j + index_left + N
if left % 2 == 0:
return fix(left,D)
left = D*j + index_left - N
if left % 2 == 1:
return fix(left,D)
return -1
def getRight(index_K, D, N):
if index_K == 0:
index_right = D-1
else:
index_right = index_K - 1
j = 0
right = D*j + index_right + N
if right % 2 == 0:
return fix(right,D)
right = D*j + index_right - N
if right % 2 == 1:
return fix(right,D)
return -1
def solve(caseId, D, K, N):
if K % 2 == 0:
index_K = (K - N) % D # K is at position (K-N)%D
left = getLeft (index_K, D, N)
right = getRight (index_K, D, N)
else:
index_K = (K + N) % D
left = getLeft (index_K, D, N)
right = getRight (index_K, D, N)
msg = "Case #"+ str(caseId) + ": " + str(left) + " " + str(right)
print msg
with open("B-large-practice.in", "r") as myFile:
T = eval(myFile.readline())
for line in myFile:
D, K, N = line.split(' ')
solve (case, eval(D), eval(K), eval(N))
case = case + 1
myFile.close()
03 martie 2017
Java8 (Streams, filtering) & Unit testing - exemplu dragut
package ch.makery.address; import ch.makery.address.model.Person; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.*; /** * Created by Geo on 3/3/2017. */ @RunWith(value = Parameterized.class) public class StreamTest { public Map<String, List<Person>> people; @Before
public void init () { people = new HashMap<>(); people.put("Bucharest", new ArrayList<Person>()); people.put("Pitesti", new ArrayList<Person>()); people.put("Timisoara", new ArrayList<Person>()); people.put("Iasi", new ArrayList<Person>()); people.put("Cluj", new ArrayList<Person>()); people.get("Bucharest").add(new Person("Geo", "T", "Bucharest", 51081, 1969, 2, 21)); people.get("Bucharest").add(new Person("Kate", "M", "Bucharest", 97124, 1999, 2, 21)); people.get("Bucharest").add(new Person("Alex", "W", "Bucharest", 51081, 1999, 2, 21)); people.get("Bucharest").add(new Person("Ana", "T", "Bucharest", 51081, 2009, 2, 21)); people.get("Pitesti").add(new Person("Lena", "A", "Pitesti", 70503, 2009, 2, 21)); people.get("Timisoara").add(new Person("Catalin", "F", "Timisoara", 51081, 2000, 2, 21)); people.get("Iasi").add(new Person("Petri", "T", "Iasi", 51081, 1976, 2, 21)); people.get("Cluj").add(new Person("Sahar", "A", "Cluj", 97124, 1977, 2, 21)); } @Parameterized.Parameter(value=0) public int age; @Parameterized.Parameter(value=1) public String city; @Parameterized.Parameter(value=2) public List<Integer> postalCodes; @Parameterized.Parameter(value=3) public String expected; @Parameterized.Parameters(name = "{index}: testFilterPersons({0},{1},{2}) => {3}") public static Collection<Object[]> data() { return Arrays.asList(new Object[][]{ {18, "Bucharest", Arrays.asList(51081), "Geo T\nAlex W\n"}, {18, "Cluj", Arrays.asList(51081, 97124), "Sahar A\n"} }); } @Test
public void testFilterPersons() { assert(filterPersons(age,city,postalCodes).equals(expected)); } private String filterPersons(int age, String city, List<Integer> postalCodes) { // why void?
return (people.get(city)).stream()
.filter( person -> postalCodes.contains(person.getPostalCode()) && person.getAge() >= age) .map ( person -> person.getFirstName() + " " + person.getLastName()) .reduce ("", (a,b) -> a+b+"\n"); } }
Abonați-vă la:
Postări (Atom)