Skip to main content

Posts

Showing posts with the label string

terraform iterate over string

ingress = “22:192.168.0.0/24:tcp,80:172.16.120.0/16:tcp,8080:0.0.0.0/0:tcp” egress = “22:192.168.0.0/24:tcp,80:172.16.120.0/16:tcp,8081:127.0.0.0/0:udp” resource “openstack_compute_secgroup_v2” “secgroup_1” { name = “secgroup” description = “my security group” count = “${length(split(“,”,var.ingress))}” rule { from_port = “${element(split(“:”,element(split(“,”,var.ingress),count.index)), 0)}” to_port = “${element(split(“:”,element(split(“,”,var.ingress),count.index)), 0)}” ip_protocol = “${element(split(“:”,element(split(“,”,var.ingress),count.index)), 2)}” cidr = “${element(split(“:”,element(split(“,”,var.ingress),count.index)), 1)}” } } Note : This will create multiple security groups if you want single security group and multiple rules use following code: resource “openstack_networking_secgroup_v2” “secgroup” { name = “secgroup” description = “My neutron security group” } resource “openstack_networking_secgroup_rule_v2” “secgroup_rule_ingress” { count = “${length(spl...

terraform iterate over string

ingress = "22:192.168.0.0/24:tcp,80:172.16.120.0/16:tcp,8080:0.0.0.0/0:tcp" egress = "22:192.168.0.0/24:tcp,80:172.16.120.0/16:tcp,8081:127.0.0.0/0:udp" resource "openstack_compute_secgroup_v2" "secgroup_1" { name = "secgroup" description = "my security group" count = "${length(split(",",var.ingress))}" rule { from_port = "${element(split(":",element(split(",",var.ingress),count.index)), 0)}" to_port = "${element(split(":",element(split(",",var.ingress),count.index)), 0)}" ip_protocol = "${element(split(":",element(split(",",var.ingress),count.index)), 2)}" cidr = "${element(split(":",element(split(",",var.ingress),count.index)), 1)}" } } Note : This will create multiple security groups if you want single security group and multiple rules use following code: resource "openstack_networ...

Java 8 Coding challenge: Swapping Characters

Your are given a string of length  N . You have to follow some rounds of swapping in the string until the below explained condition is reached. In each round the, you have to swap the  ith and (i+1)th character  , then  (i+2)th and (i+3)th  character and continue till the end of string. In each round , a character from the left will be locked i.e not available for swapping after this round , thus after some rounds of swapping , all the characters will be locked at their final position to stop any more rounds of swapping . See the sample explanation for more clarity. Input The first line of input contains a  T , the number of test case. The first line of each Test case will contain an Integer  N  , denoting the length of the string. The second line of each Test case will contain a string of length  N . Output For each and every test case, output the string after all positions are locked. Constraints 0< T <100 0< N ...

Java 8 Coding challenge: Swapping Characters

Your are given a string of length N . You have to follow some rounds of swapping in the string until the below explained condition is reached. In each round the, you have to swap the ith and (i+1)th character , then (i+2)th and (i+3)th character and continue till the end of string. In each round , a character from the left will be locked i.e not available for swapping after this round , thus after some rounds of swapping , all the characters will be locked at their final position to stop any more rounds of swapping . See the sample explanation for more clarity. Input The first line of input contains a T , the number of test case. The first line of each Test case will contain an Integer N , denoting the length of the string. The second line of each Test case will contain a string of length N . Output For each and every test case, output the string after all positions are locked. Constraints 0< T <100 0< N <10000 SAMPLE INPUT 2 6 abcdef 3 abc SAMPLE OUTPUT bdfeca bca E...

Java 8 coding challenge: Addition ain't simple

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules: Every  i th  character of string will be added to every  i th  character of reverse string. Both string will contain only lower case alphabets(a-z). Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details) Input: First line contains a value  N  denoting number of test cases. Next  N  lines contains string  str . Output: For every string  str  output the string that Jack's friends wants from him. Constraints 1 <= N <= 10 1 <= str <= 10^5 SAMPLE INPUT     4 hello codeapocalypse programming world SAMPLE OUTPUT     wqxqw hhtdmqrrqmdthh wfxtebetxfw aajaa Code: import java.io.*; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; p...

Java 8 coding challenge: Addition ain't simple

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules: Every i th character of string will be added to every i th character of reverse string. Both string will contain only lower case alphabets(a-z). Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details) Input: First line contains a value N denoting number of test cases. Next N lines contains string str . Output: For every string str output the string that Jack's friends wants from him. Constraints 1 <= N <= 10 1 <= str <= 10^5 SAMPLE INPUT 4 hello codeapocalypse programming world SAMPLE OUTPUT wqxqw hhtdmqrrqmdthh wfxtebetxfw aajaa Code: import java.io.*; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; public class AdditionAintSimple { static class Print { private final BufferedWriter bw; public Print() { this.bw = new Buffere...

Java 8 Coding Challenge: COUNT NUMBERS

Your task is pretty simple , given a string  S  , find the total count of numbers present in the digit. Input The first line contains  T  , the number of test cases. The first line of each and every testc ase will contain a integer  N  , the length of the string . The second line of each and every test case will contain a string  S  of length  N . Output For each and every testcase , output the total count of numbers present in the string. Constraints 0<T<200 0<N<10000 SAMPLE INPUT     1 26 sadw96aeafae4awdw2wd100awd SAMPLE OUTPUT     4   Explanation For the first test case , the string given is "sadw96aeafae4awdw2wd100awd". There are total of 4 numbers in the string - [96,4,2,100]. So , we output 4. Code import java.io.BufferedReader; import java.io.InputStreamReader; class TestClass { public static void main(String args[] ) throws Exception { Buf...

Java 8 Coding Challenge: COUNT NUMBERS

Your task is pretty simple , given a string S , find the total count of numbers present in the digit. Input The first line contains T , the number of test cases. The first line of each and every testc ase will contain a integer N , the length of the string . The second line of each and every test case will contain a string S of length N . Output For each and every testcase , output the total count of numbers present in the string. Constraints 0<T<200 0<N<10000 SAMPLE INPUT 1 26 sadw96aeafae4awdw2wd100awd SAMPLE OUTPUT 4 Explanation For the first test case , the string given is "sadw96aeafae4awdw2wd100awd". There are total of 4 numbers in the string - [96,4,2,100]. So , we output 4. Code import java.io.BufferedReader; import java.io.InputStreamReader; class TestClass { public static void main(String args[] ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); boolean bit...

Java 8 coding challenge: Palindromic String

Problem: You have been given a String  S S . You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String  S S . The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints   1 ≤ | S | ≤ 100 1≤|S|≤100 Note String  S S  consists of lowercase English Alphabets only. SAMPLE INPUT     aba SAMPLE OUTPUT     YES Code: /* IMPORTANT: Multiple classes and nested static classes are supported */ /* * uncomment this if you want to read input. //imports for BufferedReader import java.io.BufferedReader; import java.io.InputStreamReader; */ //import for Scanner and other utility classes import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { /* * Read ...

Java 8 coding challenge: Palindromic String

Problem: You have been given a String S S . You need to find and print whether this string is a palindrome or not. If yes, print "YES" (without quotes), else print "NO" (without quotes). Input Format The first and only line of input contains the String S S . The String shall consist of lowercase English alphabets only. Output Format Print the required answer on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100 Note String S S consists of lowercase English Alphabets only. SAMPLE INPUT aba SAMPLE OUTPUT YES Code: /* IMPORTANT: Multiple classes and nested static classes are supported */ /* * uncomment this if you want to read input. //imports for BufferedReader import java.io.BufferedReader; import java.io.InputStreamReader; */ //import for Scanner and other utility classes import java.util.*; class TestClass { public static void main(String args[] ) throws Exception { /* * Read input from stdin and provide input before running * Use either of these method...

Java 8 coding challenge: Toggle String

Problem: You have been given a String S S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output. Input Format The first and only line of input contains the String S S Output Format Print the resultant String on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100 where | S | |S| denotes the length of string S S .   SAMPLE INPUT abcdE SAMPLE OUTPUT ABCDe   Code: import java.util.Scanner; import java.lang.StringBuilder; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc= new Scanner(System.in); String s,s1,sx; char[] temp=new char[100]; s=sc.nextLine(); int x=s.length(); for(int i=0;i<x;i++){ char c = s.charAt(i); s1=Character.toString(c); if(Character.isLowerCase(c)){ sx=s1.toUp...

Java 8 coding challenge: Toggle String

Problem: You have been given a String  S S  consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output. Input Format The first and only line of input contains the String  S S Output Format Print the resultant String on a single line. Constraints 1 ≤ | S | ≤ 100 1≤|S|≤100   where  | S | |S|  denotes the length of string  S S . SAMPLE INPUT abcdE SAMPLE OUTPUT ABCDe Code: import java.util.Scanner; import java.lang.StringBuilder; class TestClass { public static void main(String args[] ) throws Exception { Scanner sc= new Scanner(System.in); String s,s1,sx; char[] temp=new char[100]; s=sc.nextLine(); int x=s.length(); for(int i=0;i<x;i++){ char c = s.charAt(i); s1=C...