我对正则表达式感兴趣,因为它找到了以下模式.
I或E表示第1个字母,N或S表示第2个,F表示第3个,第4个表示J或P表示.
这将识别ISTJ,ESTP,ENTP但不识别EJPT.
谢谢
我正在读这本书.Javascript,道格拉斯·克罗克福德的优秀作品.书中提供了一些例子,但我无法理解这些例子在实践中的用处和方式.为简单起见,我在这里修改了代码.这里有两种方法,我可以对变量进行函数赋值.
例1:
var test= function(ex) {
alert(ex);
};
test(5);
Run Code Online (Sandbox Code Playgroud)
这会生成值为5的警报框
例2:
var test1 = function test2(ex) {
alert(ex);
};
test1(7); //this produces alert box with value of 7
test2(8)//this does not give a alert box
Run Code Online (Sandbox Code Playgroud)
我已经定义了函数test2但是将它分配给了test1.为什么我不能通过调用test2(8)直接访问test2.此外,我没有看到示例2中的任何大优势超过示例1.如果您有一些差异,其中一个是优越的,我想听到.
谢谢
我遇到了这段代码,从给定的链表中删除了偶数长度的字符串我不明白为什么迭代器对象itr没有用new关键字实例化.这是代码..
public static void removeEvenLength(List<String> list) {
Iterator<String> itr= list.iterator();
while (itr.hasNext()) {
String element=itr.next();
if (element.length()%2==0) {
i.remove();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着迭代器方法是static,它只返回一个新iterable object的listas作为它field.有人可以提供一个或多个例子,其中我在Java中遇到类似的实例化方法而不是单例构造函数.谢谢
我正在使用多线程来解决经典的生产者 - 消费者问题.我正在使用wait()和notifyAll()在代码中.我的问题是,当notifyAll通知其他等待线程恢复时,它不会立即恢复.这是为什么?代码如下
public class ConsumerProducer {
private int count;
public synchronized void consume() {
while (count == 0) { // keep waiting if nothing is produced to consume
try {
wait(); // give up lock and wait
} catch (InterruptedException e) {
// keep trying
}
}
count--; // consume
System.out.println(Thread.currentThread().getName() + " after consuming " + count);
}
public synchronized void produce() {
count++; //produce
System.out.println(Thread.currentThread().getName() + " after producing " + …Run Code Online (Sandbox Code Playgroud) 我有以下代码来计算素数
def isPrime(n):
if (n==2):
return True
elif n<=1 or n%2==0:
return False
else:
for i in xrange(3,n/2, 2):
if n%i:
return False
return True
mylist = [6,9]
mylist2= [3,5,7,11,12]
if not any(isPrime(x) for x in mylist):
print "No primes in list"
if not all(isPrime(x) for x in mylist2):
print "Not all are primes numbers"
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时,我明白了
python calculate_primes.py
Not all are primes numbers
Run Code Online (Sandbox Code Playgroud)
我没有得到的输出No primes in list.但如果我删除元素9中mylist,只有有6,它工作正常.
python calculate_primes.py
No primes in …Run Code Online (Sandbox Code Playgroud) 我希望在a中打开位数BitSet.这是一个计算偶数的程序.当然,有更简单的方法来计算偶数,这只是为了理解如何使用BitSets.这是代码:
Public class Test {
public static void main(String[] args) {
BitSet b = new BitSet();
for (int i=0; i<10;i++){
b.set(i);
}
System.out.println(b);
System.out.println("even numbers ");
int i =0;
while(i<10){
if (i%2!=0){
b.clear(i);
}
i++;
}
System.out.println(b);
System.out.println(b.length());
}
}
output:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
even numbers
{0, 2, 4, 6, 8}
9
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以打开比特数,例如,在上面的例子中它应该是5.我总是可以遍历BitSet并检查是否(b.set(i))会这样o(n).有没有更快的方法来获得开启位数?
谢谢
我正在编写一个Queue包含大多数操作列表的类.但是我不会从中升级list,因为我不想提供所有的list API's.我的代码粘贴在下面.该add方法似乎工作正常,但iadd似乎出错,它打印没有.这是代码:
import copy
from iterator import Iterator
class Abstractstruc(object):
def __init__(self):
assert False
def __str__(self):
return "<%s: %s>" %(self.__class__.__name__,self.container)
class Queue(Abstractstruc,Iterator):
def __init__(self,value=[]):
self.container=[]
self.size=0
self.concat(value)
def add(self, data):
self.container.append(data)
def __add__(self,other):
return Queue(self.container + other.container)
def __iadd__(self,other):
for i in other.container:
self.add(i)
def remove(self):
self.container.pop(0)
def peek(self):
return self.container[0]
def __getitem__(self,index):
return self.container[index]
def __iter__(self):
return Iterator(self.container)
def concat(self,value):
for i in value:
self.add(i)
def __bool__(self):
return len(self.container)>0 …Run Code Online (Sandbox Code Playgroud) 看来这是return status code从你的角度来看的一种方式main.但我想知道这int是怎么回事?我在eclipse上尝试过,但是我没有在控制台上看到它.我怎么得到的status code
public static void main(String[] args){
int status = 123;
System.exit(status);
}
Run Code Online (Sandbox Code Playgroud) 我有这个模型:
class Searches(models.Model):
user = models.ForeignKey(User)
.....other fields
Run Code Online (Sandbox Code Playgroud)
每个user都有某些保存的搜索。我想显示一个form(a choiceSelect widget)以选择多个搜索之一,然后单击go。我只想filter在form中显示searches特定的User。这就是我尝试过的。但request.user下面不起作用。如何解决这个问题?
这是我的表格:
class SearchesForm(forms.Form):
#get the queryset for the searches model of logged in user
qset=Searches.objects.filter(user=request.user) ----> error
#get the most recent entry
default=Searches.objects.latest('id')
choices = forms.ModelChoiceField(queryset=qset,label='select search',initial=default)
Run Code Online (Sandbox Code Playgroud)
编辑:我的看法:
def display_search_user(request):
form=SearchesdiffForm(request)
if request.method=='POST':
search=SearchesdiffForm(request,data=request.POST)
print search
return HttpResponse(search)
return render(request,'search_list.html',{'form':form})
Run Code Online (Sandbox Code Playgroud) 我遇到了与此处发布的类似问题。。在Ajax请求期间,我试图放置加载图标和灰色背景的背景图像,不知何故,灰色背景不会延伸到页面底部。当我滚动时,它就消失了。我尝试了该帖子中建议的补救措施,但没有任何帮助。我想重新插入一个全新的 div,覆盖整个 html 正文,并使用加载图标将背景灰色化。但我真的不知道该怎么做。所以问题是使用Jquery
我的 HTML 主体包含各种元素。
使当前内容不可见或变灰,放置一个新的 div 覆盖整个 HTML 正文(基本上在当前 HTML 正文的顶部)并用灰色背景填充它
有什么帮助吗?
我使用MultiMap的Guava库.我创建了一个示例MultiMap.当我打印地图的大小时,我得到4但实际值(我认为)应该是2.为什么这个差异?
import java.util.Collection;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class MultiMapTest {
public static void main(String... args) {
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
System.out.println(myMultimap);
// Getting the size
int size = myMultimap.size();
System.out.println(size); // 4
// Getting values
Collection<String> fruits = myMultimap.get("Fruits");
System.out.println(fruits); // [Bannana, Apple, Pear]
Collection<String> vegetables = myMultimap.get("Vegetables");
System.out.println(vegetables); // [Carrot]
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Fruits=[Bannana, Apple, Pear], Vegetables=[Carrot]}
4
[Bannana, …Run Code Online (Sandbox Code Playgroud) 给定2×2矩阵,返回可能的不同岛尺寸
例如,应返回以下矩阵[5, 7].
1 0 0 0 1
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
这是一个相当简单的问题.我使用相同大小的布尔访问矩阵并以DFS方式遍历矩阵.我在这里实现了它.出于某种原因,我得到了输出[1].我试过调试,但我的思绪现在停止了工作.我相信我错过了一些愚蠢的东西.
public class IslandConnectedCell {
public static void main(String[] args) {
int[][] input = {
{1,0,0,0,1},
{1,1,1,1,1},
{0,0,0,0,0},
{1,1,0,1,1}
};
dfsIsland(input);
}
public static void dfsIsland(int[][] input) {
int rows = input.length;
int cols = input[0].length;
List<Integer> countList = new ArrayList<>();
boolean visited[][] = new boolean[rows][cols];
for (int row = 0; row < …Run Code Online (Sandbox Code Playgroud) 我试图.jpg使用requestspython中的模块从URL 下载文件.这是我试过的.没有错误.但是我无法打开输出文件.
>>> import requests
>>> l = requests.get("http://www.mosta2bal.com/vb/imgcache/2/9086screen.jpg")
>>> l
<Response [200]>
>>> l.text
u'\ufffd\ufffd\ufffd\ufffd\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\ufffd\ufffd\x12EExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x......long text
>>> l.encoding
>>> import codecs
>>> f = codecs.open('out.jpg', mode="w", encoding="utf-8")
>>> f.write(l.text)
Run Code Online (Sandbox Code Playgroud) java ×6
object ×3
python ×3
add ×1
algorithm ×1
any ×1
bitset ×1
class ×1
css ×1
django ×1
django-forms ×1
django-users ×1
exit ×1
file-io ×1
function ×1
guava ×1
html ×1
if-statement ×1
in-place ×1
instance ×1
iterator ×1
javascript ×1
jquery ×1
map ×1
new-operator ×1
notify ×1
php ×1
regex ×1
system.exit ×1
unicode ×1
wait ×1