checking for a contains in a specific string in cosmosDB graph using pythongremlin api - python-3.x

I have a 2 "WEEK" vertices on azure cosmosdb graph.
g.V().hasLabel('WEEK').valueMap()
output:
{
"type":["1 week|1 month|1 wk|one month|one week|one wk"]
},
{
"type":["11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"]
}
i am trying to search CONTAINS of a STRING in the "type" property and return the vertices.
STRING = "1 week"
g.V().hasLabel('WEEK').has('type',TextP.containing('1 week')).valueMap()
output:
{
"type":["1 week|1 month|1 wk|one month|one week|one wk"]
},
{
"type":["11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"]
}
i am getting all the vertices because "11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks" also have '1 week' in it.
my requirement is that i have to search for the contains operation but only 1st vertex should be present not the second one.
one idea can be changing the data in the "type" property and change the search string as below
g.V().hasLabel('WEEK').valueMap()
output:
{
"type":["(1 week)|(1 month)|(1 wk)|(one month)|(one week)|(one wk)"]
},
{
"type":["(11 weeks)|(11 months)|(11 wks)|(eleven months)|(eleven weeks)|(eleven wks)"]
}
STRING = "(1 week)"
g.V().hasLabel('WEEK').has('type',TextP.containing('(1 week)')).valueMap()
output:
{
"type":["(1 week)|(1 month)|(1 wk)|(one month)|(one week)|(one wk)"]
}
but this way we need to change the entire data in the "type" property and have to change the STRING as well from "1 week" to "(1 week)" (as "1 week" is received from upstream)
Please let me know any other ideas for the above scenario (doing contains is mandatory)
Thanks in advance.

You could place a | at the beginning of the data and then look for
STRING= "|1 week"

You could search for items that contain your search-string + have the same amount of characters?

Your question is kind of confusing, but something like the below should return ONLY LIST[0] being as it's only LIST[2]. It'll only return the string in the first array position which would be LIST[0]
public string[] LIST = { "1 week|1 month|1 wk|one month|one week| one wk","11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"};
public string STRING = "1 Week";
public int LISTLength = LIST.Length;
for(int x = 0; x < LISTLength; x++)
{
if (LIST[x] == STRING)
{
//Your action here
}
}

Related

Making sure every Alphabet is in a string (Kotlin)

So I have a question where I am checking if a string has every letter of the alphabet in it. I was able to check if there is alphabet in the string, but I'm not sure how to check if there is EVERY alphabet in said string. Here's the code
fun isPangram (pangram: Array<String>) : String {
var panString : String
var outcome = ""
for (i in pangram.indices){
panString = pangram[i]
if (panString.matches(".^*[a-z].*".toRegex())){
outcome = outcome.plus('1')
}
else {outcome = outcome.plus('0')}
}
return outcome
}
Any ideas are welcomed Thanks.
I think it would be easier to check if all members of the alphabet range are in each string than to use Regex:
fun isPangram(pangram: Array<String>): String =
pangram.joinToString("") { inputString ->
when {
('a'..'z').all { it in inputString.lowercase() } -> "1"
else -> "0"
}
}
Hi this is how you can make with regular expression
Kotlin Syntax
fun isStrinfContainsAllAlphabeta( input: String) {
return input.lowercase()
.replace("[^a-z]".toRegex(), "")
.replace("(.)(?=.*\\1)".toRegex(), "")
.length == 26;
}
In java:
public static boolean isStrinfContainsAllAlphabeta(String input) {
return input.toLowerCase()
.replace("[^a-z]", "")
.replace("(.)(?=.*\\1)", "")
.length() == 26;
}
the function takes only one string. The first "replaceAll" removes all the non-alphabet characters, The second one removes the duplicated character, then you check how many characters remained.
Just to bounce off Tenfour04's solution, if you write two functions (one for the pangram check, one for processing the array) I feel like you can make it a little more readable, since they're really two separate tasks. (This is partly an excuse to show you some Kotlin tricks!)
val String.isPangram get() = ('a'..'z').all { this.contains(it, ignoreCase = true) }
fun checkPangrams(strings: Array<String>) =
strings.joinToString("") { if (it.isPangram) "1" else "0" }
You could use an extension function instead of an extension property (so it.isPangram()), or just a plain function with a parameter (isPangram(it)), but you can write stuff that almost reads like English, if you want!

Understanding Graph, Weighted method

Okay, so what does the SET stand for in the second line? Why is the second string in<>, ?
public Weighted(In in, String delimiter) {
st = new ST<String, SET<String>>();
while (!in.isEmpty()) {
String line = in.readLine();
String[] names = line.split(delimiter);
for (int i = 1; i < names.length; i++) {
addEdge(names[0], names[i]);
}
}
}
With the little information you gave, I will assume that SET is an abstract data type. An abstract data type can store any values without any particular order and with no duplicates. By telling <String> after SET you are telling you want to store Strings inside your SET.
You can learn more about SETs here: https://en.wikipedia.org/wiki/Set_(abstract_data_type)

Scala: convert each digit in a string to an integer

I want to convert each digit in a number to an int. Here is my code
for (in <- lines) {
for (c <- in) {
val ci = c.toInt
if (ci == 0) {
// do stuff
}
}
}
The result I get is the ascii code, i.e. a 1 gives 49. I'm looking for the value 1.
The answer is trivial, I know. I'm trying to pull myself up with my own bootstraps until my Scala course begins in two weeks. Any assistance gratefully accepted.
One possible solution is:
for(in <- lines) {
in.toString.map(_.asDigit).foreach { i =>
if(i == 1) {
//do stuff
}
}
}
And more compact w/ output:
lines.foreach(in => in.toString.map(_.asDigit).filter(_ == 1).foreach(i => println(s"found $i in $in.")))
If lines is already a collection of Strings, omit the .toString on in.toString.
You can have this:
val number = 123456
//convert Int to String and do transformation for each character to Digit(Int)
val digitsAsList = number.toString.map(_.asDigit)
This will result to digitizing the number. Then with that Collection, you can do anything from filtering, mapping, zipping: you can checkout the the List api on this page: http://www.scala-lang.org/api/2.11.8/#scala.collection.immutable.List
Hope that's help.

swift/parse: incrementing strings

The Swift part of the question:
So what I mean by incrementing strings is that say we start off with var string = "title" I want to be able to increment numbers to the end of that like "title1", "title2", "title3...". Should I use a for loop to do this? If so, how? Or another method?
for var i = 1; i < 6; i = i + 1 {
//increment the strings here
}
The Parse part of the question:
I want to have my objectForKey use the many different titles and numbers we will produce above so that the objectForKey will be "title1", "title2", "title3"... I would make multiple columns on Parse with names " title1, title2, title3 and the cells in the tableview would correspond to that data. So cell1 would use title1's data, cell2 will use title2's data and so on. Will it work like this?
var output1 = object.objectForKey(i) as! String
A loop in Swift is like for i in 1...5, and then you can use string interpolation to get the correct string like this:
for i in 1...5 {
let title = "title\(i)"
print(title)
}
Also read Dan's answer.
There are a few ways of looping in Swift, but you should keep in mind that and as of Swift 3,
this will no longer be one of them:
for var i = 0; i <6; i++ {
let string = "title\(i+1)"
}
source : Swift Evolution
Swift's preferred way of general looping is, as GvS stated:
for i in 1...5 {
let title = "title\(i)"
}
However, you are also welcome to use Swifts higher order functions to loop:
(1...5).forEach { i in
let title = "title \(i)"
}
or
(1...5).forEach { let title = "title \($0)" }

Count the number of frequency for different characters in a string

i am currently tried to create a small program were the user enter a string in a text area, clicks on a button and the program counts the frequency of different characters in the string and shows the result on another text area.
E.g. Step 1:- User enter:- aaabbbbbbcccdd
Step 2:- User click the button
Step 3:- a 3
b 6
c 3
d 1
This is what I've done so far....
public partial class Form1 : Form
{
Dictionary<string, int> dic = new Dictionary<string, int>();
string s = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
s = textBox1.Text;
int count = 0;
for (int i = 0; i < s.Length; i++ )
{
textBox2.Text = Convert.ToString(s[i]);
if (dic.Equals(s[i]))
{
count++;
}
else
{
dic.Add(Convert.ToString(s[i]), count++);
}
}
}
}
}
Any ideas or help how can I countinue because till now the program is giving a run time error when there are same charachter!!
Thank You
var lettersAndCounts = s.GroupBy(c=>c).Select(group => new {
Letter= group.Key,
Count = group.Count()
});
Instead of dic.Equals use dic.ContainsKey. However, i would use this little linq query:
Dictionary<string, int> dict = textBox1.Text
.GroupBy(c => c)
.ToDictionary(g => g.Key.ToString(), g => g.Count());
You are attempting to compare the entire dictionary to a string, that doesn't tell you if there is a key in the dictionary that corresponds to the string. As the dictionary never is equal to the string, your code will always think that it should add a new item even if one already exists, and that is the cause of the runtime error.
Use the ContainsKey method to check if the string exists as a key in the dictionary.
Instead of using a variable count, you would want to increase the numbers in the dictionary, and initialise new items with a count of one:
string key = s[i].ToString();
textBox2.Text = key;
if (dic.ContainsKey(key)) {
dic[key]++;
} else {
dic.Add(key, 1);
}
I'm going to suggest a different and somewhat simpler approach for doing this. Assuming you are using English strings, you can create an array with capacity = 26. Then depending on the character you encounter you would increment the appropriate index in the array. For example, if the character is 'a' increment count at index 0, if the character is 'b' increment the count at index 1, etc...
Your implementation will look something like this:
int count[] = new int [26] {0};
for(int i = 0; i < s.length; i++)
{
count[Char.ToLower(s[i]) - int('a')]++;
}
When this finishes you will have the number of 'a's in count[0] and the number of 'z's in count[25].

Resources