Monthly Archives: 5月 2017

「Java」正規表現でIPアドレスのフォーマットをチェックするサンプルコード

Javaコード
public static boolean ipCheck(String text) {
if (text != null && !text.isEmpty()) {
//正規表現
String regex = “^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.”
“(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.”
“(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.”
“(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$”;
// IPアドレスのチェック
if (text.matches(regex)) {
// some code
return true;
} else {
// some code
return false;
}
}
return false;
}

「python」2つの日付の日数を数える方法

pythonコード
#!/usr/bin/python
import time
import sys
def dateinput():
date = raw_input(‘please input the first date: ‘)
return date
def datetrans(tdate):
spdate = tdate.replace(“/”,”-“)
try:
datesec = time.strptime(spdate,’%Y-%m-%d’)
except ValueError:
print “%s is not a rightful date!!” % tdate
sys.exit(1)
return time.mktime(datesec)
def daysdiff(d1,d2):
daysec = 24 * 60 * 60
return int(( d1 – d2 )/daysec)
date1 = dateinput()
date2 = dateinput()
date1sec = datetrans(date1)
date2sec = datetrans(date2)
print “The number of days between two dates is: “,daysdiff(date1sec,date2sec

「Python」8桁のパスワードを生成するサンプルコード

pythonコード
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import string
#方法1
seed = “1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-”
sa = []
for i in range(8):
sa.append(random.choice(seed))
salt = ”.join(sa)
print salt
#方法2
salt = ”.join(random.sample(string.ascii_letters + string.digits, 8))
print salt

「Python」日付フォーマット(datetime)のサンプルコード

サンプルコード
import time
print(time.strftime(“%Y-%m-%d %H:%M:%S”), time.localtime())

「python入門」テキストファイルにパスワードを生成するコード

pythonコード
#!/usr/bin/python
import string
letters = string.letters + string.digits + string.punctuation
length = len(letters)
fwrite = open(“/tmp/generatepwd.txt”,”wt”)
fread = open(“/tmp/generatepwd.txt”,”r”)
for num in xrange(8):
for times in xrange(length**num):
line=fread.read(num+1).rstrip()
for letter in letters:
fwrite.write(line + letter + “\n”)
fwrite.flush()
fwrite.close()
fread.close()

「java入門」java.util.ArrayList.get()のサンプルコード

Javaコード

import java.util.ArrayList;

public class ArrayListGetDemo {

public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList<Integer> cflist = new ArrayList<Integer>(5);

// use add() method to add elements in the list
cflist.add(23);
cflist.add(24);
cflist.add(45);
cflist.add(86);

// let us print all the elements available in list
for (Integer number : cflist) {
System.out.println(“Value = ” + number);
}

// retrieves element at 4th postion
int retval=cflist.get(3);
System.out.println(“Retrieved element is = ” + retval);
}

}

結果

Value = 23
Value = 24
Value = 45
Value = 86
Retrieved element is = 86

「java入門」java.util.ArrayDeque.descendingIterator()のサンプルコード

Javaコード

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class DescendingIteratorDemo {

public static void main(String[] args) {
// create an empty array deque with an initial capacity
Deque<Integer> cft= new ArrayDeque<Integer>(8);

// use add() method to add elements in the deque
cft.add(36);
cft.add(42);
cft.add(55);
cft.add(60);

//iterator() is used to print all the elements
//next() returns the next element on each iteration
System.out.println(“iteratorで要素を出力:”);

for(Iterator<Integer> itr = cft.iterator();itr.hasNext();) {
System.out.println(itr.next());
}

//descendingIterator() is used to print the elements in reverse order
//next() returns the next element on each iteration
System.out.println(“要素を逆順:”);

for(Iterator<Integer> descItr =cft.descendingIterator();descItr.hasNext();) {
System.out.println(descItr.next());
}
}

}

結果

iteratorで要素を出力:
36
42
55
60
要素を逆順:
60
55
42
36

「java入門」java.util.ArrayDeque.peek()のサンプルコード

javaコード

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayListPeekDemo {

public static void main(String[] args) {
// create an empty array deque with an initial capacity
Deque<Integer> chang = new ArrayDeque<Integer>(10);

// use add() method to add elements in the deque
chang.add(44);
chang.add(37);
chang.add(28);
chang.add(98);

// this will retrieve head of the queue
int retval = chang.peek();
System.out.println(“Retrieved Element is ” + retval);

// printing all the elements available in deque
for (Integer numVal : chang) {
System.out.println(“Value = ” + numVal);
}
}

}

結果

Retrieved Element is 44
Value = 44
Value = 37
Value = 28
Value = 98

「java入門」java.util.ArrayDeque.iterator()のサンプルコード

Javaコード

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class ArrayListIeratorDemo {

public static void main(String[] args) {
// create an empty array deque with an initial capacity
Deque<Integer> cft = new ArrayDeque<Integer>(8);

// use add() method to add elements in the deque
cft.add(20);
cft.add(32);
cft.add(45);
cft.add(29);

// iterator() is used to print all the elements
// next() returns the next element on each iteration
System.out.println(“printing elements using iterator:”);
for(Iterator<Integer> kd = cft.iterator();kd.hasNext();) {
System.out.println(kd.next());
}
}

}

結果

printing elements using iterator:
20
32
45
29

「Java入門」java.util.ArrayList.addAll()のサンプルコード

Javaコード

import java.util.ArrayList;

public class AddlistAllDemo {

public static void main(String[] args) {
// create an empty array list1 with an initial capacity
ArrayList<Integer> cftlist = new ArrayList<Integer>(5);

// use add() method to add elements in the list
cftlist.add(20);
cftlist.add(30);
cftlist.add(55);

// let us print all the elements available in list1
System.out.println(” list1の要素下記:”);
for (Integer numVal : cftlist) {
System.out.println(“Value = ” + numVal);
}

// create an empty array list2 with an initial capacity
ArrayList<Integer> cftlist2 = new ArrayList<Integer>(5);

// use add() method to add elements in list2
cftlist2.add(15);
cftlist2.add(20);
cftlist2.add(21);
cftlist2.add(75);

// let us print all the elements available in list2
System.out.println(“list2の要素下記:”);
for (Integer numVal : cftlist2) {
System.out.println(“Value = ” + numVal);
}

// inserting all elements, list2 will get printed after list1
cftlist.addAll(cftlist2);

System.out.println(“すべての要素下記:”);
// let us print all the elements available in list1
for (Integer numVal : cftlist) {
System.out.println(“Value = ” + numVal);
}
}

}

結果

list1の要素下記:
Value = 20
Value = 30
Value = 55
list2の要素下記:
Value = 15
Value = 20
Value = 21
Value = 75
すべての要素下記:
Value = 20
Value = 30
Value = 55
Value = 15
Value = 20
Value = 21
Value = 75

3 / 1612345...10...最後 »